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. Event loop in library
QtWS25 Last Chance

Event loop in library

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 3.0k 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.
  • directx11D Offline
    directx11D Offline
    directx11
    wrote on last edited by
    #1

    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.

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

      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 ?

      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
      • JKSHJ Offline
        JKSHJ Offline
        JKSH
        Moderators
        wrote on last edited by
        #3

        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)

        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

        1 Reply Last reply
        0
        • directx11D Offline
          directx11D Offline
          directx11
          wrote on last edited by
          #4

          > 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.

          1 Reply Last reply
          0
          • directx11D Offline
            directx11D Offline
            directx11
            wrote on last edited by
            #5

            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?

            JKSHJ 1 Reply Last reply
            0
            • directx11D directx11

              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?

              JKSHJ Offline
              JKSHJ Offline
              JKSH
              Moderators
              wrote on last edited by
              #6

              @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.

              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

              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