you need to start your QThread's event loop, see QThread::exec()
No, this is false. The socket has to have affinity with a thread with a running event loop to function properly or it needs to use the family of waitFor*() methods if not event loop is available.
Qt Code:
#include <QtGui> #include <QtNetwork> Q_OBJECT public: ~Thread() { delete manager; } void run(){ manager = new QNetworkAccessManager; connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(processReply(QNetworkReply*))); exec(); } public slots: void process(){ } void processReply(QNetworkReply *reply){ qDebug() << reply->size(); reply->deleteLater(); } private: QNetworkAccessManager *manager; }; #include "main.moc" int main(int argc, char **argv){ Thread thread; thread.moveToThread(&thread); thread.start(); return app.exec(); }To copy to clipboard, switch view to plain text mode
jan (2nd October 2009)
Is the purpose of this code to download the same page every 5 seconds?
Yes. You wanted an example of QNetworkAccessManager working with a thread event loop, you didn't mention it should do something useful.
Would
work instead ofQt Code:
connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(processReply(QNetworkReply*)), Qt::QueuedConnection);To copy to clipboard, switch view to plain text mode
?Qt Code:
connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(processReply(QNetworkReply*)));To copy to clipboard, switch view to plain text mode
They are not equivalent. The timer causes the first request to be sent.
bob2oneil (10th March 2011)
Contributor from the French Qt community from developpez.com
* Forum
* FAQ Qt ( > 100 QR)
* Advanced and Beginner Tutorials
Thanks for confirmation.
I investigate, and i understand all QObject have an affinity with a QThread and not the interfaced thread( that I believed), ans if you do
you can't call start() by signal/slot system to start a thread because the QThread eventloop don't run.Qt Code:
thread.moveToThread(&thread);To copy to clipboard, switch view to plain text mode
is right?
Contributor from the French Qt community from developpez.com
* Forum
* FAQ Qt ( > 100 QR)
* Advanced and Beginner Tutorials
You can, but then you have to move the thread to itself after start() is executed - for example from a slot connected to the started() signal.
yan (7th October 2009)
Contributor from the French Qt community from developpez.com
* Forum
* FAQ Qt ( > 100 QR)
* Advanced and Beginner Tutorials
Bookmarks