[webkit-qt] how to optimizing this code

Alexis Menard alexis.menard at openbossa.org
Wed Mar 16 06:46:14 PDT 2011


On Wednesday 16 March 2011 09:49:32 周鹏 wrote:
> Hi, all!

Hello,

>      This is my code(get content of dynamic page,load page from local file
> system,include all of the resource, and ouput )
> 
> class Nam: public QNetworkAccessManager
> {
>     Q_OBJECT
> protected:
>     virtual QNetworkReply* createRequest(Operation op, const
> QNetworkRequest & req, QIODevice * outgoingData = 0) {
>         QString path;
>         ......//convert the url of req to the path of the resource in the
> local file system QNetworkRequest newReq(req);
>         QUrl url(path);
>         newReq.setUrl(url);
>         return QNetworkAccessManager::createRequest(op, newReq,
> outgoingData); }
> };
> 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 content;
>      ......//read html code from local file system
>      QUrl url("http://blog.people.com.cn");//base url of the page
>      m_page.mainFrame()->setHtml(content, url);
>      return ;
> }
> void WebCapture::saveResult(bool ok)
> {
>     if (!ok)
>     {
>         emit finished();
>         return;
>     }
>     qDebug() << m_page.mainFrame()->toHtml();
>     emit finished();
> }
> int main(int argc, char* argv[])
> {
>     QApplication a(argc, argv);
>      WebCapture wc;
>      QObject::connect(&wc, SIGNAL(finished()), QApplication::instance,
> SLOT(quit())); wc.get;
>      return a.exec();
> }
> In average,parse a page need 140ms, but 130ms is spend on a.exec(),so, i
> have two question: 1) Is there any way to avoid call
> QApplication::exec(),because it's time-consuming.

exec() is a blocking call which runs the event loop of Qt. Of course it's 
needed. And of course it spend time inside. But if you use callgrind and 
KCacheGrind you can go done the tree and see where exactly the time is spent.

> 2) When i call a.exec(),
> does the rendering progress is called??I don't need to layout this page.

Without calling exec the network stack won't work because it needs the event 
loop. You need to look for somewhere else.

> 
>         Zhou Peng
>         zhoupeng19 at 126.com
>           2011-03-16


More information about the webkit-qt mailing list