Event loop in library



  • Hi
    I'm writing shared library (dll under Windows so far) and got problem with event loop. I want to use timer events, so i need this loop enabled. I'm trying to call QCoreApplication::exec() method inside new thread created inside library, but it returns with value -1. My overal structure looks like that:

    class Worker : public QObject
    {
        Q_OBJECT
    
    public:
        Worker()
        {
            timer.setInterval(2000);
            connect(&timer, SIGNAL(timeout()), SLOT(timeout()));
            timer.start();
        }
    
    private slots:
        void timeout()
        {
        }
    
    private:
        QTimer timer;
    };
    
    class XPThread : public QThread
    {
        Q_OBJECT
    
        void run() Q_DECL_OVERRIDE {
            Worker  work;
            int res = QCoreApplication::exec();
        }
    };
    

    Any help is highly appreciated.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Why not call QThread's exec function ?

    And since you are using a worker object, why not use the Worker Object technique described here ?


  • Moderators

    Hi @directx11,

    Is your DLL going to be used by a program that already runs Qt?

    • If yes, then you cannot use QCoreApplication in your thread. Use a QThread, like @SGaist suggested.
    • If no, then you must instantiate a QCoreApplication in your thread first, before you call QCoreApplication::exec(). (Also, you cannot use QThread. Use a std::thread instead)


  • > Is your DLL going to be used by a program that already runs Qt?

    I do not know really. Application is provided yet, i'm just writting plugin for it and i want to use Qt in it. Will check it and be back with results.



  • Looks fine right now regarding instantiating QCoreApplication object. Noticed, that events are working, when they are created in the same thread as QCoreApplication, but others, from main thread do not. From the other side cannot move objects from main thread to working one due to some OpenGL issues. So, it won't be possible to use events in main thread even after creation of QCoreApplication object or send events from one thread to another? Everything has to be done in working thread?


  • Moderators

    @directx11 said:

    Noticed, that events are working, when they are created in the same thread as QCoreApplication, but others, from main thread do not.

    How do you create those events? Please show us your code.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.