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. [SOLVED] Timers cannot be started from another thread

[SOLVED] Timers cannot be started from another thread

Scheduled Pinned Locked Moved General and Desktop
qmlqthreadqquickwindow
7 Posts 4 Posters 30.7k 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.
  • B Offline
    B Offline
    BPie
    wrote on last edited by BPie
    #1

    I have a class that has few members.

    class A : public QObject{
      Q_OBJECT
    public:
      A(QThread* th, Device* d, QObject* parent=0);
    private:
      QQmlApplicationEngine*      mEngine;
      QQuickWindow*               mWindow;
    public slots:
      void showWindow()
    };
    A::A(QThread* th, Device* d,QObject*)
      :QObject()
    {
      moveToThread(th);
      // ... registering data, loading qml, connecting to devices...
    }
    

    In an another class that is a main window I create class A and show it. It works but when I close it and show again, the windows appears but It does not response to data sent by signals, nor to mouse events.

    I got warning:
    ?libQt5Qml.so.5? QObject::startTimer: Timers cannot be started from another thread
    while invoking

    mWindow->show();
    

    What is the problem here?

    C 1 Reply Last reply
    0
    • B BPie

      I have a class that has few members.

      class A : public QObject{
        Q_OBJECT
      public:
        A(QThread* th, Device* d, QObject* parent=0);
      private:
        QQmlApplicationEngine*      mEngine;
        QQuickWindow*               mWindow;
      public slots:
        void showWindow()
      };
      A::A(QThread* th, Device* d,QObject*)
        :QObject()
      {
        moveToThread(th);
        // ... registering data, loading qml, connecting to devices...
      }
      

      In an another class that is a main window I create class A and show it. It works but when I close it and show again, the windows appears but It does not response to data sent by signals, nor to mouse events.

      I got warning:
      ?libQt5Qml.so.5? QObject::startTimer: Timers cannot be started from another thread
      while invoking

      mWindow->show();
      

      What is the problem here?

      C Offline
      C Offline
      code_fodder
      wrote on last edited by
      #2

      @BPie It is difficult to see without being able to see more of your code (calling code to A, when mWindow and mEngine are set, showWindow(), etc...).

      But this sort of error/warning can easily occur if you create/instantiate objects that you have pointers to before you call moveToThread (for example) such that they are actually within a different thread when you come to run it.

      Do you have a timer somewhere? maybe you can show some more code :)

      1 Reply Last reply
      0
      • B Offline
        B Offline
        BPie
        wrote on last edited by
        #3

        I cannot provide full code :( I don't have any timer as a member. All members are created after moveToThread and it works till it's closed and shown again. Maybe some ideas how can I debug it? The error is in the "mWindow->show()" that is invoked in slot showWindow.

        1 Reply Last reply
        0
        • JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by JKSH
          #4

          All GUI classes (including widgets, QML and Qt Quick classes) cannot be moved to another thread. They must stay in the GUI thread (which is the thread that created QApplication/QGuiApplication)

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

          1 Reply Last reply
          0
          • B Offline
            B Offline
            BPie
            wrote on last edited by
            #5

            Thank You. Is it mentioned in documentation?

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

              Hi,

              Yes it is, here

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

              JKSHJ 1 Reply Last reply
              0
              • SGaistS SGaist

                Hi,

                Yes it is, here

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

                And also http://doc.qt.io/qt-5/threads-qobject.html#qobject-reentrancy

                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