[Qt-interest] load url using QWebPage in a thread
Jason H
scorp1us at yahoo.com
Tue Apr 20 21:40:38 CEST 2010
Non-GUI threads (there is only one) cannot use GUI things.
page->mainFrame( )->load( url); isn't right
You need to collect the page and return it to the GUI thread. You can use QNetworkAccessManager to get the page in a thread. Also, you can use QNetworkAccessManager for multiple requets in one (the GUI) thread, since it is non-blocking. The performance differences should be minimal.
________________________________
From: Tarandeep Singh <tarandeep at gmail.com>
To: qt-interest at trolltech.com
Sent: Tue, April 20, 2010 1:15:49 PM
Subject: [Qt-interest] load url using QWebPage in a thread
Hi,
I am trying to build a multi threaded server that loads a
url in QWebPage in a separate thread. But I am not able to get it
working. I am creating the QWebPage in the run method of the thread. Is
this right way? How do I connect QWebPage's loadFinished signal to find
out the page has been loaded (so I can do further processing, in my
case, get the HTML of the loaded page)
Here is the code snippet.
/////////// MyServer extends QTcpServer /////////////////
MyServer::MyServer(QObject *parent) : QTctServer( parent) { }
: QTcpServer(parent)
{
}
void MyServer::incomingConnection(
int socketDescriptor)
{
QString url( "http://www.dummyurl.com/");
MyThread *thread = new MyThread( socketDescriptor, url, this);
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
thread->start();
}
///////////// MyThread ////////////////////
MyThread::MyThread( int socketDescriptor, const QString &urlstr, QObject *parent)
: QThread(parent), socketDescriptor(socketDescriptor), urlstring(urlstr)
{
}
void MyThread::run()
{
QTcpSocket tcpSocket;
if (!tcpSocket.setSocketDescriptor(socketDescriptor)) {
emit error(tcpSocket.error());
return;
}
QUrl url( urlstring);
QWebPage *page = new QWebPage( );
page->mainFrame( )->load( url);
//// HOW TO WAIT TILL LOADING FINISHED
//// GET THE html of the page
QString html = page->mainFrame( )->toHtml( );
//// OTHER STUFF
tcpSocket.write( returnValue);
tcpSocket.disconnectFromHost();
tcpSocket.waitForDisconnected();
}
//////////// main.cpp //////////////////
int main(int argc, char *argv[])
{
QApplication app(argc, argv, false);
QApplication::setGraphicsSystem( "raster");
app.setStyle( new QCleanlooksStyle( ));
MyServer server;
server.listen( QHostAddress( ip), portNo))
return app.exec();
}
Thanks,
Tarandeep
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100420/4c577d62/attachment.html
More information about the Qt-interest-old
mailing list