Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QApplication in library project
QtWS25 Last Chance

QApplication in library project

Scheduled Pinned Locked Moved Unsolved General and Desktop
libraryqapplicationqeventloop
17 Posts 3 Posters 4.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • SlaneS Offline
    SlaneS Offline
    Slane
    wrote on last edited by
    #1

    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 ?

    jsulmJ 1 Reply Last reply
    0
    • SlaneS Slane

      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 ?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #2

      @Slane You simply have to make sure you use QEventLoop only after instantiating QApplication.
      Your lib should actually use the event loop started in main app (a.exec()).

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • SlaneS Offline
        SlaneS Offline
        Slane
        wrote on last edited by Slane
        #3

        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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          Any chances of having static objects from QObject based classes ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          SlaneS 1 Reply Last reply
          0
          • SGaistS SGaist

            Hi,

            Any chances of having static objects from QObject based classes ?

            SlaneS Offline
            SlaneS Offline
            Slane
            wrote on last edited by Slane
            #5

            @SGaist In which part ? Core or GUI ?

            Core have:

            static CoreApp* CoreApp::CoreAppInstance();
            static void TerminateCoreApp();

            static CoreApp* instance;

            GUI haven't static methode and objects.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              That doesn't matter, you should not have static QObject based objects.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              2
              • SlaneS Offline
                SlaneS Offline
                Slane
                wrote on last edited by Slane
                #7

                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 ?

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  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.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • SlaneS Offline
                    SlaneS Offline
                    Slane
                    wrote on last edited by
                    #9

                    I see... so I haven't static object... I don't find the problem...

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      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.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0
                      • SlaneS Offline
                        SlaneS Offline
                        Slane
                        wrote on last edited by Slane
                        #11

                        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.

                        jsulmJ 1 Reply Last reply
                        0
                        • SlaneS Slane

                          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.

                          jsulmJ Offline
                          jsulmJ Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          @Slane What is CoreApp?

                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                          SlaneS 1 Reply Last reply
                          0
                          • jsulmJ jsulm

                            @Slane What is CoreApp?

                            SlaneS Offline
                            SlaneS Offline
                            Slane
                            wrote on last edited by
                            #13

                            @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;
                            };
                            
                            1 Reply Last reply
                            0
                            • SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              What in Database since it's where you seem to have trouble ?

                              Interested in AI ? www.idiap.ch
                              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                              1 Reply Last reply
                              0
                              • SlaneS Offline
                                SlaneS Offline
                                Slane
                                wrote on last edited by Slane
                                #15

                                Minimal code that don't work :

                                In exe :

                                GUI.pro

                                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

                                CoreApp.pro

                                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)

                                1 Reply Last reply
                                0
                                • SGaistS Offline
                                  SGaistS Offline
                                  SGaist
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #16

                                  Since you modified your code, what do you mean by "doesn't work" ?

                                  Interested in AI ? www.idiap.ch
                                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  SlaneS 1 Reply Last reply
                                  0
                                  • SGaistS SGaist

                                    Since you modified your code, what do you mean by "doesn't work" ?

                                    SlaneS Offline
                                    SlaneS Offline
                                    Slane
                                    wrote on last edited by Slane
                                    #17

                                    @SGaist I wish mean throw this message : QEventLoop: Cannot be used without QApplication

                                    But this problem is no more. I have delete the project and create un other without dll...
                                    It's not the best solution but it's gona work.

                                    1 Reply Last reply
                                    0

                                    • Login

                                    • Login or register to search.
                                    • First post
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • Users
                                    • Groups
                                    • Search
                                    • Get Qt Extensions
                                    • Unsolved