Qt -Timers can only be used with threads running with QThread

My code

class ExampleTest : public QObject
{
    Q_OBJECT

public:
    ExampleTest() {}

private Q_SLOTS:
   void DoAllExampleTests();
};

void ExampleTest::DoAllExampleTests()
{
    QProcess p;

    p.start( "cmd /c wmic path Win32_usbcontrollerdevice|grep VID_1004" );
    qDebug() << "Here 1";
    QVERIFY( TRUE == p.waitForFinished() );
    qDebug() << "Here 2";
}

QTEST_APPLESS_MAIN(ExampleTest);

I get qwarn between here 1 and here 2

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

I learned from QObject :: startTimer: timers can only be used with threads running with QThread , which, when subclassing a Qt class and one of the members of the subclass, is not part of the Qt hierarchy. I have an ExampleTest class inherited from QObject, but still I get a warning. How to avoid this warning?

+4
source share
1 answer

A warning may use better wording - this is not exactly a QThread problem, it is an event loop problem. QThread automatically installs one for you, but here you only have the main thread.

:

2. unit test. , unit test QApplication, , QTEST_APPLESS_MAIN. :

main(), TestClass.

QTEST_MAIN(), QApplication . GUI.

.

, , , :

QTEST_APPLESS_MAIN(ExampleTest);

:

QTEST_MAIN(ExampleTest);

... .

+8

Source: https://habr.com/ru/post/1588872/


All Articles