[webkit-qt] can't receive signal

周鹏 zhoupeng19 at 126.com
Wed Apr 6 06:27:15 PDT 2011


Dear webkit-qt, 
   I use this code to parse the redirect html page,but i have two question,i can't fix them:
1)the initialLayoutCompleted signal is never emit in my program;
2)i just catch the loadFinished signal,and quit,but when the first time i call parse function,it's work correctly,but the second time,it's blocked,it's can't catch the done() signal,it's can't quit exec().Can any one tell me how to resolve this?

class NetworkAccessManagerProxy : public QNetworkAccessManager 
{
    Q_OBJECT
public:
    virtual QNetworkReply* createRequest(Operation op, const QNetworkRequest& request, QIODevice *outgoingData)
 {
        NetworkReply* reply = new NetworkReply();
        reply->setHttpStatusCode(200, "ok" );
     reply->setContentType("text/html");
     reply->setContent("");
     return reply;
 }
};
class QtParser : public QObject
{
    Q_OBJECT
public:
    QtParser(int timeout);
    int parse(const char* content, const char* start_url, std::string* target, bool completelyParse = false); 
 void output();
public slots:
    void loadDone(bool ok);
    void layoutDone();
    void timeoutSlot();
    void urlChanged(const QUrl &url);
private:
 QApplication* m_app;
 QTimer* m_timer;
 NetworkAccessManagerProxy* m_nam;
    QWebPage* m_page;
    bool m_layoutFinished;
    bool m_loadFinished;
    int m_timeout;
signals:
    void finished();
    void timeout();
    void done();
    void error();
};
int argc = 1;
char* argv[] = {"JSParser"};
QtParser::QtParser(int timeout): QObject()
{
 m_app = new QApplication(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);
    QObject::connect(this, SIGNAL(done()), m_app, SLOT(quit()));
    QObject::connect(this, SIGNAL(error()), m_app, SLOT(quit()));
    QObject::connect(this, SIGNAL(timeout()), m_app, SLOT(quit()));
    m_page = new QWebPage();
 QObject::connect(m_page->mainFrame(), SIGNAL(loadFinished(bool)), this, SLOT(loadDone(bool)));
 QObject::connect(m_page->mainFrame(), SIGNAL(urlChanged(const QUrl &)), this, SLOT(urlChanged(const QUrl &)));
 QObject::connect(m_page->mainFrame(), SIGNAL(initialLayoutCompleted()), this, SLOT(layoutDone()));//the initialLayoutCompleted signal is never emit in this program
 m_nam = new NetworkAccessManagerProxy();
 m_page->setNetworkAccessManager(m_nam);
 m_timer = new QTimer(this);
 m_timeout = timeout;
 returnCode = 0;
}
void QtParser::parse(const char* content, const char* url)
{
 m_layoutFinisahed = false;
 m_loadFinished = false;
 QObject::connect(m_timer, SIGNAL(timeout()), this, SLOT(timeoutSlot()));
 m_timer->start(m_timeout);
 m_page->mainFrame()->setHtml(QString(content), QUrl(url));
 m_app->exec();
 return ;
}
void QtParser::urlChanged(const QUrl &url)
{
 qDebug() << url;
}
void QtParser::layoutDone()
{
 m_layoutFinished = true;
 output();
}
void QtParser::loadDone(bool ok)
{
    if (!ok) 
    {
        emit error(); 
        return; 
    }
    m_loadFinished = true;
    m_layoutFinished = true;//because the initialLayoutCompleted is never emit,so i must use this code to driven the program 
    //output();
 emit done();
}
void QtParser::timeoutSlot()
{
 m_timer->stop();
 emit timeout();
}

void QtParser::output()
{
 if(m_loadFinished && m_layoutFinished)
    {
     m_timer->stop();
     emit done();
    }
}
int main(int argc, char **argv)
{
 QtParser parser(500);
 //source is the html code of the page
 parser.parse(source, url);
 parser.parse(source, url);//my program is blocked at here
 return 0;
}
<html xmlns="http://www.w3.org/1999/xhtml"> 
 <head> 
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  <title></title> 
 </head>  
 <body> 
  <script language="javascript"> location.replace("default/index.aspx") </script> 
 </body> 
</html>


Sincerely yours, 
Zhou Peng 
zhoupeng19 at 126.com 
2011-04-06 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.webkit.org/pipermail/webkit-qt/attachments/20110406/86c4230e/attachment-0001.html>


More information about the webkit-qt mailing list