Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Unsolved Event loop in library

    General and Desktop
    3
    6
    2492
    Loading More Posts
    • 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.
    • directx11
      directx11 last edited by

      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 Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        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 Reply Quote 0
        • JKSH
          JKSH Moderators last edited by

          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 Reply Quote 0
          • directx11
            directx11 last edited by

            > 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 Reply Quote 0
            • directx11
              directx11 last edited by

              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?

              JKSH 1 Reply Last reply Reply Quote 0
              • JKSH
                JKSH Moderators @directx11 last edited by

                @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 Reply Quote 0
                • First post
                  Last post