[SOLVED] Timers cannot be started from another thread



  • 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?



  • @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 :)



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


  • Moderators

    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)



  • Thank You. Is it mentioned in documentation?


  • Lifetime Qt Champion

    Hi,

    Yes it is, here


  • Moderators


Log in to reply
 

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