Re: [Cppcms-users] CppCMS and QT
Brought to you by:
artyom-beilis
From: Artyom <art...@ya...> - 2010-12-30 21:10:32
|
> > my actual problem is that i want to use the QNetworkAccessManager in a > cppcms application object to perform an external https call which is > required for an authentication to an external 3rd party provider. > actually i do something like this in the cppcms application object: > > QNetworkAccessManager * nmgr = new QNetworkAccessManager(); > QNetworkReply * nrep = nmgr->get(QNetworkRequest(url)); > if(!nrep->waitForReadyRead(30000)) { > // read failed > } else { > QByteArray json = nrep->readAll(); > // do something with the json data.. > } > > when i run this i get the following runtime error from qt: > > QObject::startTimer: QTimer can only be used with threads started with > QThread > > this must originate inside the waitForReadyRead call since the > QNetworkAccessManager works asynchronous.. > > the problem according to the error message must be that the thread is > not created through QThread and therefore misses some supplemental data > which hinders QTimer on working. so my first thought was to gt the > cppcms application somehow inside a QThread. of course i have no idea if > that's possible at all.. I had found this: http://doc.trolltech.com/qq/qq27-responsive-guis.html#waitinginalocaleventloop Basically it is an example you may use as draft. You need to have QCoreApplication created. This simple example taken from Qt site works fine, even not from the main thread: #include <QNetworkAccessManager> #include <QNetworkReply> #include <QNetworkRequest> #include <QUrl> #include <QEventLoop> #include <QTimer> #include <QCoreApplication> #include <iostream> void *func(void *) { QNetworkAccessManager manager; QEventLoop q; QTimer tT; tT.setSingleShot(true); QObject::connect(&tT, SIGNAL(timeout()), &q, SLOT(quit())); QObject::connect(&manager, SIGNAL(finished(QNetworkReply*)), &q, SLOT(quit())); QNetworkReply *reply = manager.get(QNetworkRequest( QUrl("http://www.google.com"))); tT.start(5000); // 5s timeout q.exec(); if(tT.isActive()){ // download complete std::cout << reply->readAll().data() << std::endl; tT.stop(); } else { std::cout << "Timeout" << std::endl; // timeout } return 0; } int main(int argc,char **argv) { QCoreApplication app(argc,argv); pthread_t pid; pthread_create(&pid,0,func,0); pthread_join(pid,0); } Best, Artyom |