c++ - 需要一个关于如何使用 QThreadPool 的工作示例

标签 c++ qt qt-creator qtwebkit

我想将多线程 webkit 与 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();
}

我的线程.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)

我该如何解决?谁能举个例子? 谢谢

最佳答案

我建议你看看

http://wiki.qt.io/Threads_Events_QObjects

注意线程之间的 QObjects 所有权。

关于c++ - 需要一个关于如何使用 QThreadPool 的工作示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12542815/

相关文章:

qt - 如何获取 QTableWidget 光标下的行

Qt Stream 网络摄像机视频

c++ - 使用 Qt 在 Mac 上设置表单行,如 iTunes 信息对话框

android - 带有下一步/后退按钮的 Qt 对话框/小部件

qt4 - 如何找到Qt的版本?

c++ - 如何在 C++ 中遍历句子中的单词?

c++ - 此断点当前不会被命中 : Dll debugging

Qt Creator 编译后不会运行应用程序

c++ - 多态覆盖

C++ 堆栈和二维数组