Skip to content

Commit 9ecceff

Browse files
committed
Qt example: Updated to follow advice from @alexallmont
1 parent d171fd2 commit 9ecceff

File tree

5 files changed

+56
-54
lines changed

5 files changed

+56
-54
lines changed

CMakeLists.txt

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,9 @@ else()
3333
-Wpedantic # warn if non-standard C++ is used
3434
-Wconversion # warn on type conversions that may lose data
3535
-Wsign-conversion # warn on sign conversions
36-
#-Wmisleading-indentation # warn if identation implies blocks where blocks
37-
# do not exist
38-
#-Wduplicated-cond # warn if if / else chain has duplicated conditions
39-
#-Wduplicated-branches # warn if if / else branches have duplicated code
36+
#-Wmisleading-indentation # warn if identation implies blocks where blocks do not exist
37+
#-Wduplicated-cond # warn if if / else chain has duplicated conditions
38+
#-Wduplicated-branches # warn if if / else branches have duplicated code
4039
-Wlogical-op # warn about logical operations being used where bitwise were
4140
# probably wanted
4241
#-Wnull-dereference # warn if a null dereference is detected
@@ -61,12 +60,7 @@ add_test(Tester tester)
6160
#qt
6261
if( DEFINED CPP_STARTER_USE_QT )
6362
message( "Using Qt" )
64-
find_package( Qt5Widgets REQUIRED )
65-
set( CMAKE_AUTOMOC ON )
66-
add_executable( helloQt qt/qtMain.cpp qt/HelloQt.cpp )
67-
#set_target_properties( helloQt PROPERTIES CMAKE_AUTOMOC ON )
68-
target_link_libraries( helloQt Qt5::Widgets )
69-
target_compile_features( helloQt PUBLIC cxx_nullptr cxx_lambdas )
63+
add_subdirectory(qt)
7064
endif()
7165

7266
#fltk test

qt/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
find_package( Qt5Widgets REQUIRED )
2+
set( CMAKE_AUTOMOC ON )
3+
add_executable( helloQt qtMain.cpp HelloQt.cpp )
4+
#set_target_properties( helloQt PROPERTIES CMAKE_AUTOMOC ON )
5+
target_link_libraries( helloQt Qt5::Widgets )
6+
target_compile_features( helloQt PUBLIC cxx_nullptr cxx_lambdas )

qt/HelloQt.cpp

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,43 @@
55
#include <QLabel>
66
#include <QDebug>
77

8-
HelloQt::HelloQt( QWidget* parent )
9-
:QWidget( parent )
10-
{
11-
// The memory management is not done explicitly but by using the parent relationship built into Qt
12-
// The tr calls are not strictly neccesary, but it is all that is needed for future i18n.
13-
auto mainLayout = new QVBoxLayout;
14-
auto descriptiveLabel = new QLabel( tr("Descriptive") );
15-
auto pushButton = new QPushButton( tr("Push me!") );
16-
auto grouping = new QHBoxLayout;
17-
auto directButton = new QPushButton( tr( "Directly!" ) );
18-
auto fatal = new QPushButton( tr( "Don't push this button" ) );
19-
// By adding the widgets to the main widget, they get parented and the parenting takes care of freeing the memory
20-
grouping->addWidget( descriptiveLabel );
21-
grouping->addWidget( pushButton );
22-
mainLayout->addLayout( grouping );
23-
mainLayout->addWidget( directButton );
24-
mainLayout->addWidget( fatal );
25-
setLayout( mainLayout );
26-
// Signals and slots are use to simply define relationships between actions and effects
27-
// By using this "new style" connect the types are checked at compile time to match
28-
connect( pushButton, &QPushButton::clicked, this, &HelloQt::writeToDebug );
29-
// Lambdas can also be used as can ordinary freestanding functions
30-
connect( directButton, &QPushButton::clicked, [](){ qDebug() << tr( "I am in line" ); } );
31-
// Most usefull signals and slots are already built into the framework.
32-
connect( fatal, &QPushButton::clicked, this, &HelloQt::close );
8+
HelloQt::HelloQt(QWidget *parent) : QWidget(parent) {
9+
// The memory management is not done explicitly but by using the parent
10+
// relationship built into Qt
11+
// The tr calls are not strictly neccesary, but it is all that is needed for
12+
// future i18n.
13+
auto mainLayout = new QVBoxLayout;
14+
auto descriptiveLabel = new QLabel(tr("Descriptive"));
15+
auto pushButton = new QPushButton(tr("Push me!"));
16+
auto grouping = new QHBoxLayout;
17+
auto directButton = new QPushButton(tr("Directly!"));
18+
auto fatal = new QPushButton(tr("Don't push this button"));
19+
// By adding the widgets to the main widget, they get parented and the
20+
// parenting takes care of freeing the memory.
21+
// Laying out in layouts makes the GUI automatically resize.
22+
grouping->addWidget(descriptiveLabel);
23+
grouping->addWidget(pushButton);
24+
mainLayout->addLayout(grouping);
25+
mainLayout->addWidget(directButton);
26+
mainLayout->addWidget(fatal);
27+
setLayout(mainLayout);
28+
// Signals and slots are use to simply define relationships between actions
29+
// and effects
30+
// By using this "new style" connect the types are checked at compile time to
31+
// match
32+
connect(pushButton, &QPushButton::clicked, this, &HelloQt::writeToDebug);
33+
// Lambdas can also be used as can ordinary freestanding functions
34+
connect(directButton, &QPushButton::clicked,
35+
[]() { qDebug() << tr("I am in line"); });
36+
// Most usefull signals and slots are already built into the framework.
37+
connect(fatal, &QPushButton::clicked, this, &HelloQt::close); // could just use close
3338
};
3439

35-
HelloQt::~HelloQt()
36-
{
37-
// Nothing to do here, the parent system makes sure that all the elements that are
38-
// parented to this is cleaned up
40+
HelloQt::~HelloQt() {
41+
// Nothing to do here, the parent system makes sure that all the elements that
42+
// are parented to this is cleaned up
3943
}
4044

41-
void HelloQt::writeToDebug( void )
42-
{
43-
qDebug() << tr( "I don't know what I was supposed to debug here..." );
45+
void HelloQt::writeToDebug(void) {
46+
qDebug() << tr("I don't know what I was supposed to debug here...");
4447
}
45-

qt/HelloQt.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
class HelloQt : public QWidget
44
{
5-
Q_OBJECT // Very Important, this is all the magic that is needed
6-
public:
7-
explicit HelloQt( QWidget* parent = nullptr );
8-
~HelloQt();
9-
private slots:
10-
void writeToDebug( void );
5+
Q_OBJECT // Very Important, this is all the magic that is needed
6+
public:
7+
explicit HelloQt(QWidget* parent = nullptr);
8+
~HelloQt();
9+
private slots:
10+
void writeToDebug();
1111

1212
};

qt/qtMain.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#include "HelloQt.hpp"
22
#include <QApplication>
33

4-
int main( int argc, char* argv[] )
4+
int main(int argc, char* argv[])
55
{
6-
QApplication app( argc, argv );
7-
HelloQt dialog;
8-
dialog.show();
6+
QApplication app(argc, argv);
7+
HelloQt dialog;
8+
dialog.show();
99

10-
return app.exec(); // this runs the main event loop and sees to it that cleanup is done
10+
return app.exec(); // this runs the main event loop and sees to it that cleanup is done
1111
}
1212

0 commit comments

Comments
 (0)