[webkit-qt] load url using QWebPage in a thread

Tarandeep Singh tarandeep at gmail.com
Tue Apr 20 01:32:18 PDT 2010


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.webkit.org/pipermail/webkit-qt/attachments/20100420/e9b57763/attachment-0001.html>


More information about the webkit-qt mailing list