Running the gui via QtRocket class instead of directly from main(). Intention is to have the gui run in its own thread, and be an interface to QtRocket component (which is the application), rather than the application itself
This commit is contained in:
parent
90e5289609
commit
a6845d4552
68
QtRocket.cpp
68
QtRocket.cpp
@ -1,17 +1,81 @@
|
|||||||
#include "QtRocket.h"
|
#include "QtRocket.h"
|
||||||
|
#include "gui/MainWindow.h"
|
||||||
|
|
||||||
|
#include <QApplication>
|
||||||
|
#include <QLocale>
|
||||||
|
#include <QTranslator>
|
||||||
|
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
|
|
||||||
|
// Initialize static member data
|
||||||
QtRocket* QtRocket::instance = nullptr;
|
QtRocket* QtRocket::instance = nullptr;
|
||||||
|
std::mutex QtRocket::mtx;
|
||||||
|
bool QtRocket::initialized = false;
|
||||||
|
|
||||||
|
|
||||||
|
// The gui worker thread
|
||||||
|
void guiWorker(int argc, char* argv[], int& ret)
|
||||||
|
{
|
||||||
|
QApplication a(argc, argv);
|
||||||
|
a.setWindowIcon(QIcon(":/qtrocket.png"));
|
||||||
|
|
||||||
|
// Start translation component.
|
||||||
|
// TODO: Only support US English at the moment. Anyone want to help translate?
|
||||||
|
QTranslator translator;
|
||||||
|
const QStringList uiLanguages = QLocale::system().uiLanguages();
|
||||||
|
for (const QString &locale : uiLanguages)
|
||||||
|
{
|
||||||
|
const QString baseName = "qtrocket_" + QLocale(locale).name();
|
||||||
|
if (translator.load(":/i18n/" + baseName))
|
||||||
|
{
|
||||||
|
a.installTranslator(&translator);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Go!
|
||||||
|
MainWindow w(QtRocket::getInstance());
|
||||||
|
w.show();
|
||||||
|
ret = a.exec();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
QtRocket* QtRocket::getInstance()
|
QtRocket* QtRocket::getInstance()
|
||||||
{
|
{
|
||||||
if(!instance)
|
if(!initialized)
|
||||||
{
|
{
|
||||||
instance = new QtRocket();
|
init();
|
||||||
}
|
}
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void QtRocket::init()
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> lck(mtx);
|
||||||
|
if(!initialized)
|
||||||
|
{
|
||||||
|
instance = new QtRocket();
|
||||||
|
initialized = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
QtRocket::QtRocket()
|
QtRocket::QtRocket()
|
||||||
{
|
{
|
||||||
logger = utils::Logger::getInstance();
|
logger = utils::Logger::getInstance();
|
||||||
|
running = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
int QtRocket::run(int argc, char* argv[])
|
||||||
|
{
|
||||||
|
// Really should only start this thread once
|
||||||
|
if(!running)
|
||||||
|
{
|
||||||
|
running = true;
|
||||||
|
int ret = 0;
|
||||||
|
std::thread guiThread(guiWorker, argc, argv, std::ref(ret));
|
||||||
|
guiThread.join();
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
19
QtRocket.h
19
QtRocket.h
@ -1,8 +1,11 @@
|
|||||||
#ifndef QTROCKET_H
|
#ifndef QTROCKET_H
|
||||||
#define QTROCKET_H
|
#define QTROCKET_H
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
#include <mutex>
|
||||||
|
#include <atomic>
|
||||||
|
|
||||||
#include "utils/Logger.h"
|
#include "utils/Logger.h"
|
||||||
#include "gui/MainWindow.h"
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief The QtRocket class is the master controller for the QtRocket application.
|
* @brief The QtRocket class is the master controller for the QtRocket application.
|
||||||
@ -13,15 +16,25 @@ class QtRocket
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static QtRocket* getInstance();
|
static QtRocket* getInstance();
|
||||||
|
|
||||||
|
utils::Logger* getLogger() { return logger; }
|
||||||
|
|
||||||
|
// This will return when the main window returns;
|
||||||
|
// If called multiple times, subsequent calls, will simply
|
||||||
|
// immediately return with value 0
|
||||||
|
int run(int argc, char* argv[]);
|
||||||
private:
|
private:
|
||||||
QtRocket();
|
QtRocket();
|
||||||
|
|
||||||
|
static void init();
|
||||||
|
|
||||||
|
std::atomic_bool running;
|
||||||
|
static bool initialized;
|
||||||
|
static std::mutex mtx;
|
||||||
static QtRocket* instance;
|
static QtRocket* instance;
|
||||||
|
|
||||||
utils::Logger* logger;
|
utils::Logger* logger;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // QTROCKET_H
|
#endif // QTROCKET_H
|
||||||
|
@ -10,9 +10,10 @@
|
|||||||
#include <memory>
|
#include <memory>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
|
||||||
MainWindow::MainWindow(QWidget *parent)
|
MainWindow::MainWindow(QtRocket* _qtRocket, QWidget *parent)
|
||||||
: QMainWindow(parent)
|
: QMainWindow(parent),
|
||||||
, ui(new Ui::MainWindow)
|
ui(new Ui::MainWindow),
|
||||||
|
qtRocket(_qtRocket)
|
||||||
{
|
{
|
||||||
ui->setupUi(this);
|
ui->setupUi(this);
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,11 @@
|
|||||||
#ifndef QTROCKET_H
|
#ifndef MAINWINDOW_H
|
||||||
#define QTROCKET_H
|
#define MAINWINDOW_H
|
||||||
|
|
||||||
#include <QMainWindow>
|
#include <QMainWindow>
|
||||||
|
|
||||||
|
#include "QtRocket.h"
|
||||||
|
|
||||||
|
|
||||||
QT_BEGIN_NAMESPACE
|
QT_BEGIN_NAMESPACE
|
||||||
namespace Ui { class MainWindow; }
|
namespace Ui { class MainWindow; }
|
||||||
QT_END_NAMESPACE
|
QT_END_NAMESPACE
|
||||||
@ -12,7 +15,7 @@ class MainWindow : public QMainWindow
|
|||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
MainWindow(QWidget *parent = nullptr);
|
MainWindow(QtRocket* _qtRocket, QWidget *parent = nullptr);
|
||||||
~MainWindow();
|
~MainWindow();
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
@ -24,5 +27,6 @@ private slots:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
Ui::MainWindow* ui;
|
Ui::MainWindow* ui;
|
||||||
|
QtRocket* qtRocket;
|
||||||
};
|
};
|
||||||
#endif // QTROCKET_H
|
#endif // MAINWINDOW_H
|
||||||
|
67
main.cpp
67
main.cpp
@ -1,12 +1,38 @@
|
|||||||
|
#include "QtRocket.h"
|
||||||
|
#include "utils/Logger.h"
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
|
||||||
|
// Instantiate logger
|
||||||
|
utils::Logger* logger = utils::Logger::getInstance();
|
||||||
|
logger->setLogLevel(utils::Logger::DEBUG);
|
||||||
|
// instantiate QtRocket
|
||||||
|
QtRocket* qtrocket = QtRocket::getInstance();
|
||||||
|
|
||||||
|
// Run QtRocket. This'll start the GUI thread and block until the user
|
||||||
|
// exits the program
|
||||||
|
int retVal = qtrocket->run(argc, argv);
|
||||||
|
logger->debug("Returning");
|
||||||
|
return retVal;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This was the old main when it directly started the QtGui. It worked. The new way of
|
||||||
|
* starting the gui through QtRocket->run() also seems to work, but I'm leaving this here for
|
||||||
|
* now in case there are unforeseen issues with starting the gui in a separate thread via
|
||||||
|
* QtRocket::run()
|
||||||
|
*
|
||||||
|
|
||||||
#include "gui/MainWindow.h"
|
#include "gui/MainWindow.h"
|
||||||
|
|
||||||
#include <QApplication>
|
#include <QApplication>
|
||||||
#include <QLocale>
|
#include <QLocale>
|
||||||
#include <QTranslator>
|
#include <QTranslator>
|
||||||
|
|
||||||
#include <thread>
|
int main(int argc, char* argv[])
|
||||||
|
|
||||||
void worker(int argc, char* argv[], int& ret)
|
|
||||||
{
|
{
|
||||||
QApplication a(argc, argv);
|
QApplication a(argc, argv);
|
||||||
a.setWindowIcon(QIcon(":/qtrocket.png"));
|
a.setWindowIcon(QIcon(":/qtrocket.png"));
|
||||||
@ -28,37 +54,6 @@ void worker(int argc, char* argv[], int& ret)
|
|||||||
// Go!
|
// Go!
|
||||||
MainWindow w;
|
MainWindow w;
|
||||||
w.show();
|
w.show();
|
||||||
ret = a.exec();
|
return a.exec();
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
|
||||||
{
|
|
||||||
/*
|
|
||||||
QApplication a(argc, argv);
|
|
||||||
a.setWindowIcon(QIcon(":/qtrocket.png"));
|
|
||||||
|
|
||||||
// Start translation component.
|
|
||||||
// TODO: Only support US English at the moment. Anyone want to help translate?
|
|
||||||
QTranslator translator;
|
|
||||||
const QStringList uiLanguages = QLocale::system().uiLanguages();
|
|
||||||
for (const QString &locale : uiLanguages)
|
|
||||||
{
|
|
||||||
const QString baseName = "qtrocket_" + QLocale(locale).name();
|
|
||||||
if (translator.load(":/i18n/" + baseName))
|
|
||||||
{
|
|
||||||
a.installTranslator(&translator);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Go!
|
|
||||||
//MainWindow w;
|
|
||||||
//w.show();
|
|
||||||
//return a.exec();
|
|
||||||
*/
|
|
||||||
int ret = 0;
|
|
||||||
std::thread guiThread(worker, argc, argv, std::ref(ret));
|
|
||||||
guiThread.join();
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
Loading…
x
Reference in New Issue
Block a user