QApplication in library project
-
Hello,
I have made a project splited in 2 part, (Core (dll) and GUI (exe)). The Core and GUI need to use slots and signals
but I have an error : "QEventLoop: Cannot be used without QApplication"
the Core (dll) create serveral threads and use signals to communicate. the "Main" Core is in the same thread than GUI's main()
How can I make the Core eventLoop to use the GUI's QApplication ?
-
Currently I have this structure (with big simplification)
(GUI) main.cpp
int main(int argc, char *argv[]) { QApplication a(argc, argv); Core core; core.start(); return a.exec(); }
(GUI) Core.cpp
Core::Core(): gui(new GUI()) { CoreApp::CoreAppInstance(); } void Core::start() { this->gui->show(); } Core::~Core() { CoreApp::TerminateCoreApp(); delete this->gui; }
(Dll) CoreApp.cpp
CoreApp::CoreApp() { this->database = new Database(); this->databaseThread = new QThread(); this->database->moveToThread(this->databaseThread); this->databaseThread->start(); // <------ Error trigger } CoreApp* CoreApp::CoreAppInstance() { if(CoreApp::instance == nullptr) { CoreApp::instance = new CoreApp(); } return CoreApp::instance; } void CoreApp::TerminateCoreApp() { delete CoreApp::instance; CoreApp::instance = nullptr; }
I think QEventLoop is instantiated after QApplication.
Edit :
QCoreApplication::instance() == nullptr in dll
QCoreApplication::instance() != nullptr in exe -
Hi,
Any chances of having static objects from QObject based classes ?
-
That doesn't matter, you should not have static QObject based objects.
-
Ha... so I gonna to find another way... Without static method and object...
But why we shouldn't create static object based on QObject ?
EDIT : My bad... it's not static QObject based object but static ptr over QObject based object... it's the same case ?
-
I didn't talk about static methods at all.
QApplication does some initialisation. Your static QObject based classed would be created before QApplication and thus would be in an "uninitialised" state.
No, static pointers are not concerned.
-
With the code you provided, there's nothing that can really be guessed except that you might have something wrong in your Database class.
On an unrelated note, it seems you are not cleaning anything when destroying your CoreApp instance. At least from the code you posted.
-
I can't post all the code... they are no other thing like static object based on QObject who can throw this error ?
@SGaist said in QApplication in library project:
On an unrelated note, it seems you are not cleaning anything when destroying your CoreApp instance. At least from the code you posted.
The database and thread aren't destroy by this way.
-
@jsulm CoreApp is the wrapper of all functionality in the DLL
Currently :
#include "coreapp_global.h" #include <QObject> class Database; class COREAPPSHARED_EXPORT CoreApp : public QObject { Q_OBJECT public: static CoreApp* CoreAppInstance(); static void TerminateCoreApp(); private: static CoreApp* instance; CoreApp(); ~CoreApp(); Database* database; QThread* databaseThread; };
-
What in Database since it's where you seem to have trouble ?
-
Minimal code that don't work :
In exe :
QT = core gui widgets TARGET = GUI TEMPLATE = app LIBS += -LD:\Data\Code\Projet\build-CoreApp\debug -lCoreApp DEFINES += QT_DEPRECATED_WARNINGS SOURCES += \ main.cpp \ Core.cpp HEADERS += \ Core.h
main.cpp
#include "Core.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); Core core; return a.exec(); }
core.h
#ifndef CORE_H #define CORE_H class Core { public: Core(); }; #endif // CORE_H
core.cpp
#include "Core.h" #include "../CoreApp/CoreApp.h" Core::Core() { new CoreApp(); } void Core::start() { } Core::~Core() { CoreApp::TerminateCoreApp(); }
In dll
QT += sql core TARGET = CoreApp TEMPLATE = lib DEFINES += QT_DEPRECATED_WARNINGS SOURCES += \ CoreApp.cpp \ HEADERS += \ CoreApp.h \ CoreApp_global.h \ unix { target.path = /usr/lib INSTALLS += target }
CoreApp.h
#ifndef COREAPP_H #define COREAPP_H #include "coreapp_global.h" #include <QObject> class COREAPPSHARED_EXPORT CoreApp: public QObject { Q_OBJECT public: CoreApp(); private: QThread* databaseThread; }; #endif // COREAPP_H
coreapp_gloable.h
#ifndef COREAPP_GLOBAL_H #define COREAPP_GLOBAL_H #include <QtCore/qglobal.h> #if defined(COREAPP_LIBRARY) # define COREAPPSHARED_EXPORT Q_DECL_EXPORT #else # define COREAPPSHARED_EXPORT Q_DECL_IMPORT #endif #endif // COREAPP_GLOBAL_H
CoreApp.cpp
#include "CoreApp.h" #include <QThread> #include <QDebug> #include <QApplication> CoreApp::CoreApp() { this->databaseThread = new QThread(); this->databaseThread->start(); }
I think the probleme don't come from the code...
(the code are handmodified) -
Since you modified your code, what do you mean by "doesn't work" ?