2012-07-21 114 views
1

我有這樣的代碼:QWebPage和多線程

class TestThread : public QThread 
{ 
public: 
    void run() 
    { 
     QFile file("test.html"); 
     file.open(QIODevice::ReadOnly); 
     QWebPage page; 
     page.mainFrame()->setHtml(file.readAll()); 
     qDebug() << page.mainFrame()->toHtml(); 
     qDebug() << "\n\n\n\n"; 
    } 
}; 

int main(int argc, char *argv[]) 
{ 
    QApplication a(argc, argv); 
    for(int i = 0; i < 2; ++i) 
    { 
     TestThread thread; 
     thread.start(); 
     thread.wait(); 
    } 
    return a.exec(); 
} 

輸出:

"<html><head> 
    <title>My page</title> 
    </head> 
    <body> 
     My content 

</body></html>" 





"<html><head></head><body><html> 
    <head> 
    <title>My page</title> 
    </head> 
    <body> 
     My content 
    </body> 
</html></body></html>" 

在第二遍,有太多的標籤。什麼是解決方法?或者我的錯誤在哪裏?

+0

請發佈test.html的內容。 – Troubadour 2012-07-21 20:08:51

+0

還有第三個html被寫在你實際需要的兩個之間的某處。 – 2012-07-21 20:15:01

+0

我無法編輯我的帖子。我收到消息:「您的帖子沒有太多的上下文來解釋代碼段,請更清楚地解釋您的情況。」 :-( – brainstream 2012-07-21 20:17:04

回答

0

我在Linux操作系統中遇到了問題。在Windows中,我收到了消息「窗口小部件必須在GUI線程中創建」。但QWebPage中的內容是正確的。所以,我不會爲我的任務使用QWebPage。