2012-09-22 60 views
2

我想使用多線程的WebKit與QThreadPool如何使用QThreadPool

我的代碼工作的例子是:

webkitrunnable.cpp:

webkitRunnable::webkitRunnable(QUrl inputURL) : url(inputURL) 
{ 
    init(); 
} 

void webkitRunnable::run() 
{ 
    qDebug() << "run ..."; 
    qDebug() << "webkit runnable --> " << url; 

    loadPage(url); 
} 

void webkitRunnable::init() 
{ 
    qDebug() << "WebKit--> init webkit"; 
    webView = new QWebView; 

    connect(webView, SIGNAL(loadFinished(bool)), SLOT(finishLoading(bool))); 

    manager = new QNetworkAccessManager(this); 

    manager = webView->page()->networkAccessManager(); 

    webView->page()->setNetworkAccessManager(manager); 

    connect(manager, SIGNAL(finished(QNetworkReply*)),this, SLOT(replyFinished(QNetworkReply*))); 
} 

void webkitRunnable::finishLoading(bool) 
{ 
    qDebug() << "WebKit--> finish loading"; 
} 

void webkitRunnable::replyFinished(QNetworkReply* Reply) 
{ 
    qDebug() << "WebKit--> reply Finished"; 
} 

void webkitRunnable::loadPage(QUrl url) 
{ 
    qDebug() << "WebKit--> load Page"; 
    webView->load(url); 
    webView->setFocus(); 
} 

webkitrunnable.h:

class webkitRunnable : public QObject, public QRunnable 
{ 
    Q_OBJECT 

public: 
    webkitRunnable(QUrl inputURL); 

    void loadPage(QUrl url); 

protected: 
    void run(); 

signals: 

public slots: 
    void finishLoading(bool); 
    void replyFinished(QNetworkReply*); 

private: 
    void init(); 

    QUrl     url; 
    QNetworkAccessManager *manager; 
    QWebView    *webView; 
}; 

mythread.cpp:

MyThread::MyThread(QObject *parent) : 
    QObject(parent) 
{ 
    threadPool = new QThreadPool(this); 
    threadPool->setMaxThreadCount(20); 

    webkit = new webkitRunnable(QUrl("http://www.google.com/")); 
} 

MyThread::~MyThread() 
{ 
    delete threadPool; 
} 

void MyThread::startMultiThreadLoad(QUrl url) 
{ 
    webkit = new webkitRunnable(url); 

    connect(webkit, SIGNAL(threadFinished(int)), this, SLOT(finished(int)), Qt::QueuedConnection); 

    for (int i = 0; i < threadPool->maxThreadCount(); i++) 
    { 
     threadPool->start(webkit); 
     qDebug() << "start(active):" << threadPool->activeThreadCount(); 
    } 
} 

void MyThread::finished(int number) 
{ 
    qDebug() << "thread number is: " << number; 
    qDebug() << "finished(active):" << threadPool->activeThreadCount(); 
} 

mythread.h:

class MyThread : public QObject 
{ 
    Q_OBJECT 
public: 
    explicit MyThread(QObject *parent = 0); 
    ~MyThread(); 
    void startMultiThreadLoad(QUrl url); 

public slots: 
    void finished(int); 

private: 
    webkitRunnable* webkit; 
    QUrl   globalURL; 

    QThreadPool  *threadPool; 
}; 

每當執行webkit->load(),我得到了Qt Creator中的以下應用程序輸出:

QObject: Cannot create children for a parent that is in a different thread. 
(Parent is QNetworkAccessManager(0x6f1598), parent's thread is QThread(0x65dfd0), current thread is QThread(0x6ccd28) 

哪有我解決了它?任何人都可以舉個例子嗎? 感謝

回答

1

我建議你去看看,以

http://wiki.qt.io/Threads_Events_QObjects

注重線程之間的QObject所有權。

+0

thanx很多爲您的答案 – mgh

+0

im在相同的情況下,我可以使用MoveToThread,如果我使用 ThreadPool? – user63898

+0

我對moveToThread沒有任何經驗,因爲它在我之前粘貼的鏈接中說過,這不是推薦的。但是,在這種情況下,我認爲doc很不錯:http://qt-project.org/doc/qt-4.8/qobject.html#moveToThread – kikeenrique