Skip to content

Commit

Permalink
code examples: improve qt hello world
Browse files Browse the repository at this point in the history
  • Loading branch information
danctorres committed Jan 3, 2025
1 parent 4706ce1 commit 519f30b
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 62 deletions.
23 changes: 4 additions & 19 deletions examples/qt/BUILD
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
cc_library(
name = "lib",
srcs = ["hello.cpp"],
hdrs = [
"hello.hpp",
],
cc_binary(
name = "bin",
srcs = ["main.cpp"],
copts = [
"-I/usr/include/x86_64-linux-gnu/qt5",
"-I/usr/include/x86_64-linux-gnu/qt5/QtGui",
"-I/usr/include/x86_64-linux-gnu/qt5/QtWidgets",
"-I/usr/include/x86_64-linux-gnu/qt5/QtCore",
],
Expand All @@ -15,16 +13,3 @@ cc_library(
"-lQt5Gui",
],
)

cc_binary(
name = "bin",
srcs = ["main.cpp"],
copts = [
"-I/usr/include/x86_64-linux-gnu/qt5",
"-I/usr/include/x86_64-linux-gnu/qt5/QtWidgets",
"-I/usr/include/x86_64-linux-gnu/qt5/QtCore",
],
deps = [
":lib",
],
)
18 changes: 0 additions & 18 deletions examples/qt/hello.cpp

This file was deleted.

13 changes: 0 additions & 13 deletions examples/qt/hello.hpp

This file was deleted.

64 changes: 52 additions & 12 deletions examples/qt/main.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,59 @@
#include <qapplication.h>

#include <QApplication>
#include <memory>
#include <QFont>
#include <QGridLayout>
#include <QLCDNumber>
#include <QPushButton>
#include <QSlider>
#include <QVBoxLayout>
#include <QWidget>

class LCDRange : public QWidget {
public:
explicit LCDRange(QWidget *parent = nullptr);
};

#include "hello.hpp"
LCDRange::LCDRange(QWidget *parent) : QWidget(parent) {
QLCDNumber *lcd = new QLCDNumber(2);
lcd->setSegmentStyle(QLCDNumber::Filled);

auto main(int argc, char **argv) -> int {
QApplication const app(argc, argv);
QSlider *slider = new QSlider(Qt::Horizontal);
slider->setRange(0, 99);
slider->setValue(0);
connect(slider, SIGNAL(valueChanged(int)), lcd, SLOT(display(int)));

QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(lcd);
layout->addWidget(slider);
setLayout(layout);
}

const int WindowWidth = 600;
const int WindowHeight = 600;
class MyWidget : public QWidget {
public:
explicit MyWidget(QWidget *parent = nullptr);
};

auto mainWidget = std::make_unique<MyWidget>();
mainWidget->resize(WindowWidth, WindowHeight);
mainWidget->show();
MyWidget::MyWidget(QWidget *parent) : QWidget(parent) {
QPushButton *quit = new QPushButton(tr("Quit"));
quit->setFont(QFont("Times", 18, QFont::Bold));
connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));

QGridLayout *grid = new QGridLayout;
for (int row = 0; row < 3; ++row) {
for (int column = 0; column < 3; ++column) {
auto *lcdRange = new LCDRange;
grid->addWidget(lcdRange, row, column);
}
}

QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(quit);
layout->addLayout(grid);
setLayout(layout);
}

return QApplication::exec();
auto main(int argc, char *argv[]) -> int {
QApplication app(argc, argv);
MyWidget widget;
widget.show();
return app.exec();
}

0 comments on commit 519f30b

Please sign in to comment.