[Qt-interest] load url using QWebPage in a thread

Tarandeep Singh tarandeep at gmail.com
Tue Apr 20 21:54:06 CEST 2010


On Tue, Apr 20, 2010 at 12:40 PM, Jason H <scorp1us at yahoo.com> wrote:

> 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.
>
>
Thanks Jason for the reply.

I didn't understand this clearly- "collect the page and return it to the GUI
thread". I am aware this is a method- moveToThread( ), so I should call
page->moveToThread( guiThread). But which is the GUI thread here? the main
program control flow? How do I get its thread?

Also, I checked QNetworkAccessManager api, I don't see any method that will
give me Page object.

Can you please point me to some code snippet so that I can understand this.

Appreciate your help,
Tarandeep


>
> ------------------------------
> *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/07889fd9/attachment.html 


More information about the Qt-interest-old mailing list