Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 需要一个关于如何使用QThreadPool的工作示例吗_C++_Qt_Qt Creator_Qtwebkit - Fatal编程技术网

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

C++ 需要一个关于如何使用QThreadPool的工作示例吗,c++,qt,qt-creator,qtwebkit,C++,Qt,Qt Creator,Qtwebkit,我想将多线程webkit与QThreadPool 我的代码是: webkitrunnable.cpp: webkitRunnable::webkitRunnable(QUrl inputURL) : url(inputURL) { init(); } void webkitRunnable::run() { qDebug() << "run ..."; qDebug() << "webkit runnable --> " <<

我想将多线程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();
}
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::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.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();
}
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::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();
}
无论何时执行
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)
我怎样才能解决它?有人能举个例子吗?
谢谢

我建议你去看看


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

im处于相同的情况,如果我使用ThreadPool,我可以使用MoveToThread?我对MoveToThread没有任何经验,正如我之前粘贴的链接中所说,这是不推荐的。但是,我认为doc在这种情况下很好: