需要一个关于如何使用QThreadPool的工作示例
Needing a working example on how to use 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)
我该如何解决?谁能举个例子?谢谢
我建议您查看
http://wiki.qt.io/Threads_Events_QObjects
注意线程之间的 QObject 所有权。
相关文章:
- QSqlquery prepare()和bindvalue()不工作
- 导入库可以跨dll版本工作吗
- 以螺旋方式打印矩阵的程序.(工作不好)
- 对象指针在c++中是如何工作的
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- C++为线程工作动态地分割例程
- 为什么我的 std::ref 无法按预期工作?
- 布尔比较运算符是如何在C++中工作的
- SampleConsensusPrerejective(ext.RANSAC)是如何真正工作的
- 不确定要在我的main中放入什么才能使我的代码正常工作
- 为什么std::condition_variable notify_all的工作速度比notify_one快(对于随机请
- <<操作员在下面的行中工作
- 有人能解释一下为什么下界是这样工作的吗C++的
- ExtractIconEx:可以工作,但偶尔会崩溃
- C++中的memset函数工作不正常
- 当我在第一个循环中使用"auto"时,它工作正常,但是使用"int"它会给出错误,为什么?
- 链表c++插入,所有情况都已检查,但没有任何工作
- 需要一个关于如何使用QThreadPool的工作示例