PDA

View Full Version : QTest and QEventLoop inside a library



MementoMori
22nd February 2011, 16:10
Hi all,
inside a library I have code like:


QNetworkReply* AIDAW::getRequest(const QString& url){
QEventLoop loop;
QTimer timer;

QNetworkRequest request(url);
QNetworkReply *reply = this->network_manager->get(request);

QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
QObject::connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));

timer.start(TIMEOUT);
loop.exec();

if(timer.isActive() == false){
//manage the timeout
}

return reply;

}


If I try to write a QTest Project using this library I get this error:


********* Start testing of MyProject *********
Config: Using QTest library 4.7.0, Qt 4.7.0
PASS : AIDATest::initTestCase()
QEventLoop: Cannot be used without QApplication
QObject::connect: Cannot connect (null)::aboutToQuit() to QNativeWifiEngine::clo
seHandle()
QObject::startTimer: QTimer can only be used with threads started with QThread
QObject::startTimer: QTimer can only be used with threads started with QThread


the main of the test project is managed by QTEST_MAIN(MyTestClass) macro

franz
22nd February 2011, 16:38
It is unwise to start an event loop within an event loop. The issue you run into right now could be one you run into when running a program using the library. In this case you should probably start a thread (with an event loop then) and wait until the thread has finished doing it's job. Alternatively you could try a different approach, but that would require more knowledge of the nature of your system. See also http://labs.qt.nokia.com/2010/02/23/unpredictable-exec/.