[webkit-qt] QObject: Cannot create children for a parent that is in a different thread

周鹏 zhoupeng19 at 126.com
Wed Mar 23 00:47:14 PDT 2011


Hi,all! 
  This is my program,it's create a child thread to get the content of a url,but I got the following error output:
     QObject: Cannot create children for a parent that is in a different thread.
    (Parent is QWebPage(0x94fa350), parent's thread is QThread(0x93b04a8), current thread is Thread(0xbff5d33c)

class WebCapture : public QObject
{
    Q_OBJECT
public:
    WebCapture();
    void get(QUrl url); 
signals:
    void finished();
private slots:
    void saveResult(bool ok); 
private:
    QWebPage m_page;
};
WebCapture::WebCapture(): QObject()
{
    QObject::connect(&m_page, SIGNAL(loadFinished(bool)), this, SLOT(saveResult(bool))); 
} 
void WebCapture::get(QUrl url)
{ 
    m_page.mainFrame()->load(url);
}
void WebCapture::saveResult(bool ok)
{
    if (!ok) 
    {
        emit finished(); 
        return; 
    }
    qDebug() << m_page.mainFrame()->toHtml();//output
    emit finished();
}
class Thread : public QThread
{
    Q_OBJECT
public:
    Thread();
    ~Thread();
    void run();
private:
    WebCapture wc;
};
Thread::Thread():QThread()
{
    QObject::connect(&wc, SIGNAL(finished()), this, SLOT(jobDone())); 
}
Thread::~Thread()
{
}
void Thread::jobDone()
{
    printf("exit!");
}
void Thread::run()
{
    QUrl url("http://www.google.com/"); 
    wc->get(url);
    exec();
}
#include "test.moc" 
int main(int argc, char* argv[])
{
     QApplication app(argc, argv);
     Thread th;
     th.start();
     th.wait();
     return 0;
}

        Zhou Peng 
        zhoupeng19 at 126.com 
          2011-03-23 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.webkit.org/pipermail/webkit-qt/attachments/20110323/34ba2f25/attachment.html>


More information about the webkit-qt mailing list