Nominate our 2022 Qt Champions!

[SOLVED] Timers cannot be started from another thread

  • I have a class that has few members.

    class A : public QObject{
      A(QThread* th, Device* d, QObject* parent=0);
      QQmlApplicationEngine*      mEngine;
      QQuickWindow*               mWindow;
    public slots:
      void showWindow()
    A::A(QThread* th, Device* d,QObject*)
      // ... 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:
    ? QObject::startTimer: Timers cannot be started from another thread
    while invoking


    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


    Yes it is, here

  • Moderators