
Originally Posted by
jan
I have discovered that QTcpSocket and QTcpServer has to be created in main thread in order to function properly.
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.

Originally Posted by
piotr.dobrogost
Do you know how to use QNetworkAccessManager with QEventLoop other than the one in the main thread?
#include <QtGui>
#include <QtNetwork>
Q_OBJECT
public:
Thread
() : QThread(){ manager
= 0;
} ~Thread() { delete manager; }
void run(){
manager = new QNetworkAccessManager;
connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(processReply(QNetworkReply*)));
QTimer::singleShot(0,
this,
SLOT(process
()));
exec();
}
public slots:
void process(){
QNetworkReply
*reply
= manager
->get
(QNetworkRequest
(QUrl("http://www.qtcentre.org/")));
QTimer::singleShot(5000,
this,
SLOT(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();
}
#include <QtGui>
#include <QtNetwork>
class Thread : public QThread {
Q_OBJECT
public:
Thread() : QThread(){ manager = 0; }
~Thread() { delete manager; }
void run(){
manager = new QNetworkAccessManager;
connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(processReply(QNetworkReply*)));
QTimer::singleShot(0, this, SLOT(process()));
exec();
}
public slots:
void process(){
QNetworkReply *reply = manager->get(QNetworkRequest(QUrl("http://www.qtcentre.org/")));
QTimer::singleShot(5000, this, SLOT(process()));
}
void processReply(QNetworkReply *reply){
qDebug() << reply->size();
reply->deleteLater();
}
private:
QNetworkAccessManager *manager;
};
#include "main.moc"
int main(int argc, char **argv){
QApplication app(argc, argv);
Thread thread;
thread.moveToThread(&thread);
thread.start();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks