Skip to content

Commit 79e6462

Browse files
authored
Merge pull request cpp-best-practices#6 from paulbendixen/add_qt_gui
Add qt gui
2 parents 58c9bc5 + f7e2392 commit 79e6462

File tree

5 files changed

+92
-4
lines changed

5 files changed

+92
-4
lines changed

CMakeLists.txt

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cmake_minimum_required(VERSION 3.2)
1+
cmake_minimum_required(VERSION 3.8)
22

33
option(BUILD_SHARED_LIBS "Enable compilation of shared libraries" FALSE)
44

@@ -55,23 +55,33 @@ enable_testing()
5555

5656
add_executable(tester tester.cpp)
5757
target_link_libraries(tester PRIVATE project_warnings --coverage)
58-
5958
add_test(Tester tester)
6059

60+
#qt
61+
if( DEFINED CPP_STARTER_USE_QT )
62+
message( "Using Qt" )
63+
add_subdirectory(qt)
64+
endif()
65+
6166
#fltk test
67+
if( DEFINED CPP_STARTER_USE_FLTK )
6268
find_package(FLTK REQUIRED)
6369
add_executable(test_fltk fltk/test_fltk.cpp)
6470
target_link_libraries(test_fltk PRIVATE project_warnings ${FLTK_LIBRARIES})
6571
target_include_directories(test_fltk PRIVATE ${FLTK_INCLUDE_DIR})
72+
endif()
6673

6774
# gtkmm test
75+
if( DEFINED CPP_STARTER_USE_GTKMM )
6876
find_package(PkgConfig REQUIRED)
6977
pkg_check_modules(GTKMM REQUIRED gtkmm-3.0)
7078
add_executable(test_gtkmm gtkmm/main.cpp gtkmm/hello_world.cpp)
7179
target_link_libraries(test_gtkmm PRIVATE project_warnings ${GTKMM_LIBRARIES})
7280
target_include_directories(test_gtkmm PRIVATE ${GTKMM_INCLUDE_DIRS})
81+
endif()
7382

7483
# imgui example
84+
if( DEFINED CPP_STARTER_USE_IMGUI )
7585
find_package(SFML COMPONENTS graphics window system)
7686
find_package(OpenGL)
7787

@@ -83,8 +93,10 @@ target_link_libraries(imgui INTERFACE ${SFML_LIBRARIES} ${OPENGL_gl_LIBRARY})
8393
add_executable(test_imgui imgui/test.cpp)
8494
target_link_libraries(test_imgui PRIVATE project_warnings imgui)
8595
target_include_directories(test_imgui PRIVATE ${SFML_INCLUDE_DIR})
96+
endif()
8697

8798
# Nana
99+
if( DEFINED CPP_STARTER_USE_NANA )
88100
include(ExternalProject)
89101
ExternalProject_add(
90102
Nana
@@ -99,5 +111,4 @@ ExternalProject_Get_Property(Nana SOURCE_DIR BINARY_DIR)
99111
add_executable(test_nana nana/main.cpp)
100112
target_include_directories(test_nana PRIVATE ${SOURCE_DIR}/include)
101113
target_link_libraries(test_nana PRIVATE ${BINARY_DIR}/libnana.so ${NANA_LINKS})
102-
103-
114+
endif()

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: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include "HelloQt.hpp"
2+
#include <QHBoxLayout>
3+
#include <QVBoxLayout>
4+
#include <QPushButton>
5+
#include <QLabel>
6+
#include <QDebug>
7+
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
38+
};
39+
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
43+
}
44+
45+
void HelloQt::writeToDebug(void) {
46+
qDebug() << tr("I don't know what I was supposed to debug here...");
47+
}

qt/HelloQt.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <QWidget>
2+
3+
class HelloQt : public QWidget
4+
{
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();
11+
12+
};

qt/qtMain.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include "HelloQt.hpp"
2+
#include <QApplication>
3+
4+
int main(int argc, char* argv[])
5+
{
6+
QApplication app(argc, argv);
7+
HelloQt dialog;
8+
dialog.show();
9+
10+
return app.exec(); // this runs the main event loop and sees to it that cleanup is done
11+
}
12+

0 commit comments

Comments
 (0)