Home > OS >  QObject Child Class Not Detecting QGuiApplication Event Loop
QObject Child Class Not Detecting QGuiApplication Event Loop

Time:10-26

When I try to start a QTimer in a class derived from QObject, I get the warning

QObject::startTimer: Timers can only be used with threads started with QThread

and the timer doesn't run. Based on answer here, it appears that my custom class is not detecting QEventLoop created by QGuiApplication.

My main.cpp

...
classA objA;
...
QGuiApplication app(argc, argv);
...

My classA.h

class classA : public QObject
{
   Q_OBJECT
   private: 
      QTimer m_oTimer;
...

My classA.cpp

classA::classA()
{
   ...
   m_oTimer.start(100);
   ...
}

How can I fix that without creating a new QEventLoop?

CodePudding user response:

I was able to fix the problem by changing the order of declaration of my classA and QGuiApplication. It appears that for any QObject child class to detect QGuiApplication Eventloop, it must be declared after QGuiApplication.

My main.cpp:

...
QGuiApplication app(argc, argv);
...
classA objA;
...
  • Related