[webkit-qt] multithread in QtWebKit

周鹏 zhoupeng19 at 126.com
Mon Mar 21 22:29:30 PDT 2011


Hi,all! 
   I have a program which load html(dynamic page) from local file system and execute the javascript in the page ,and output the content of the page.
This is the code:

class Nam: public QNetworkAccessManager 
{
 ......//modify the request,load resource from local file
};
class WebCapture : public QObject
{
    Q_OBJECT
public:
    WebCapture();
    void get(); 
signals:
    void finished();
private slots:
    void saveResult(bool ok); 
private:
    Nam nam;
    QWebPage m_page;
};
WebCapture::WebCapture(): QObject()
{
    m_page.setNetworkAccessManager(&nam);
    connect(&m_page, SIGNAL(loadFinished(bool)), this, SLOT(saveResult(bool))); 
} 
void WebCapture::get(QString html,QString url)
{ 
    ......
    m_page.mainFrame()->setHtml(content, url);
    return ;
}
void WebCapture::saveResult(bool ok)
{
    if (!ok) 
    {
        emit finished(); 
        return; 
    }
    qDebug() << m_page.mainFrame()->toHtml();//output
    emit finished();
}

#include "local.moc"
int main(int argc, char* argv[])
{
    ......;
    QApplication app(argc, argv);
    QWebSettings * settings = QWebSettings::globalSettings();
    settings->setAttribute(QWebSettings::AutoLoadImages, false);
    settings->setAttribute(QWebSettings::JavascriptEnabled, true);
    settings->setAttribute(QWebSettings::JavaEnabled, false);
    settings->setAttribute(QWebSettings::PluginsEnabled, false);
    settings->setAttribute(QWebSettings::PrivateBrowsingEnabled, false);
    
    WebCapture wc;
    QObject::connect(&wc, SIGNAL(finished()), QApplication::instance(), SLOT(quit()));
    wc.get(source, url);
    return app.exec();//start process
}

This program is works well,but i want to modify it to multithread,so, i have some question:
1)When the program is started,the main thread create some child thread,each child process one page,and main thread delivery task to child.As far as i know,the page process is trigger by QApplication ::exec(),so, if i used multithread, every child must have a  QApplication object, i did this in my program,but it has "Segmentation fault!" .But if i used a global QApplication object,and every child used this object,Does the childs in conflict with each other??

2)If possible,please give me some demo code.

        Zhou Peng 
        zhoupeng19 at 126.com 
          2011-03-22 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.webkit.org/pipermail/webkit-qt/attachments/20110322/2d8395b6/attachment-0001.html>


More information about the webkit-qt mailing list