(C++) QtExample30
February 24, 2017 · View on GitHub
(C++) QtExample30



Qt example 30: controlling program flow with a Singleton is a Qt Example that uses Singleton to produce dialogs until it is told to quit.
Technical facts
Operating system(s) or programming environment(s)
Lubuntu 15.04 (vivid)
Qt Creator 3.1.1
- G++ 4.9.2
Libraries used:
Qt project file: ./CppQtExample30/CppQtExample30.pro
exists(../../DesktopApplication.pri) { include(../../DesktopApplication.pri) } !exists(../../DesktopApplication.pri) { QT += core gui greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TEMPLATE = app CONFIG(debug, debug|release) { message(Debug mode) } CONFIG(release, debug|release) { message(Release mode) DEFINES += NDEBUG NTRACE_BILDERBIKKEL } QMAKE_CXXFLAGS += -std=c++11 -Wall -Wextra unix { QMAKE_CXXFLAGS += -Werror } } exists(../../Libraries/Boost.pri) { include(../../Libraries/Boost.pri) } !exists(../../Libraries/Boost.pri) { win32 { INCLUDEPATH += \ ../../../Projects/Libraries/boost_1_55_0 } } SOURCES += \ main.cpp \ firstdialog.cpp \ seconddialog.cpp \ commander.cpp HEADERS += \ firstdialog.h \ seconddialog.h \ commander.h FORMS += \ firstdialog.ui \ seconddialog.ui
./CppQtExample30/commander.h
#ifndef COMMANDER_H #define COMMANDER_H ///Commander is a Singleton for managing program flow struct Commander { ///Get the Commander's only instance static Commander * GetInstance(); ///Start the program void Run(); ///Child asks to quit the program void Quit(); private: ///A Singleton's constructor is private Commander(); ///The one and only Commander instance static Commander * m_instance; ///Flag the tell whether to quit bool m_quit; }; #endif // COMMANDER_H
./CppQtExample30/commander.cpp
#include "commander.h" #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Weffc++" #pragma GCC diagnostic ignored "-Wunused-local-typedefs" #include <boost/shared_ptr.hpp> #include "firstdialog.h" #include "seconddialog.h" #pragma GCC diagnostic pop Commander * Commander::m_instance = 0; Commander::Commander() : m_quit(false) { } Commander * Commander::GetInstance() { if (!m_instance) m_instance = new Commander; return m_instance; } void Commander::Quit() { m_quit = true; } void Commander::Run() { while (!m_quit) { boost::shared_ptr<QDialog> dialog( ((std::rand() >> 4) % 2) ? dynamic_cast<QDialog*>(new FirstDialog) : dynamic_cast<QDialog*>(new SecondDialog)); dialog->exec(); } }
./CppQtExample30/firstdialog.h
#ifndef FIRSTDIALOG_H #define FIRSTDIALOG_H #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Weffc++" #pragma GCC diagnostic ignored "-Wunused-local-typedefs" #include <QDialog> #pragma GCC diagnostic pop namespace Ui { class FirstDialog; } class FirstDialog : public QDialog { Q_OBJECT public: explicit FirstDialog(QWidget *parent = 0); FirstDialog(const FirstDialog&) = delete; FirstDialog& operator=(const FirstDialog&) = delete; ~FirstDialog(); private: Ui::FirstDialog *ui; }; #endif // FIRSTDIALOG_H
./CppQtExample30/firstdialog.cpp
#pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Weffc++" #pragma GCC diagnostic ignored "-Wunused-local-typedefs" #include "firstdialog.h" #include "ui_firstdialog.h" #pragma GCC diagnostic pop FirstDialog::FirstDialog(QWidget *parent) : QDialog(parent), ui(new Ui::FirstDialog) { ui->setupUi(this); } FirstDialog::~FirstDialog() { delete ui; }
./CppQtExample30/main.cpp
#pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Weffc++" #pragma GCC diagnostic ignored "-Wunused-local-typedefs" #include "commander.h" #include <QApplication> #pragma GCC diagnostic pop int main(int argc, char *argv[]) { QApplication a(argc, argv); Commander::GetInstance()->Run(); return 0; }
./CppQtExample30/seconddialog.h
#ifndef SECONDDIALOG_H #define SECONDDIALOG_H #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Weffc++" #pragma GCC diagnostic ignored "-Wunused-local-typedefs" #include <QDialog> #pragma GCC diagnostic pop namespace Ui { class SecondDialog; } class SecondDialog : public QDialog { Q_OBJECT public: explicit SecondDialog(QWidget *parent = 0); SecondDialog(const SecondDialog&) = delete; SecondDialog& operator=(const SecondDialog&) = delete; ~SecondDialog() noexcept; private slots: void on_pushButton_clicked(); private: Ui::SecondDialog *ui; }; #endif // SECONDDIALOG_H
./CppQtExample30/seconddialog.cpp
#pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Weffc++" #pragma GCC diagnostic ignored "-Wunused-local-typedefs" #include "seconddialog.h" #include "ui_seconddialog.h" #include "commander.h" #pragma GCC diagnostic pop SecondDialog::SecondDialog(QWidget *parent) : QDialog(parent), ui(new Ui::SecondDialog) { ui->setupUi(this); } SecondDialog::~SecondDialog() noexcept { delete ui; } void SecondDialog::on_pushButton_clicked() { Commander::GetInstance()->Quit(); this->close(); }