作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在另一个启动时启动QThread,但是它不起作用。
main.cpp片段
Worker stat_worker;
stat_worker.moveToThread(stat_worker.stat_thread);
Worker some;
some.moveToThread(some.somethread);
QObject::connect(stat_worker.stat_thread, SIGNAL(started()), some.somethread, SLOT(start()));
QObject::connect(some.somethread, SIGNAL(started()), &some, SLOT(print_some()));
stat_worker.stat_thread->start();
worker
class Worker : public QObject
{
Q_OBJECT
public:
explicit Worker();
QThread *stat_thread = new QThread;
QThread *somethread = new QThread;
signals:
//some signals
void start_thread();
public slots:
//some slots
void print_some();
void somethread_starter();
};
#endif // WORKER_H
worker.cpp相关功能
void Worker::print_some()
{
qInfo() << "-somethread started() signal arrived!";
}
当我尝试通过单击按钮启动线程时,也没有用。
QObject::connect(stat_worker.stat_thread, &QThread::started, &some, &Worker::somethread_starter);
void Worker::somethread_starter()
{
qInfo() << "-I got started by another thread!";
somethread->start();
}
或在启动另一个线程时发出的信号:
void Worker::wip_status(){
emit start_thread();
}
QObject::connect(stat_worker.stat_thread, &QThread::started, &stat_worker, &Worker::wip_status);
QObject::connect(&stat_worker, &Worker::start_thread, &some, &Worker::somethread_starter);
工作。
最佳答案
我试图用自己的MCVE(只是短一点)重现OP的问题。
#include <QtWidgets>
struct Worker: QObject {
QString name;
QThread qThread;
Worker(const QString &name): name(name)
{
moveToThread(&qThread);
connect(&qThread, &QThread::finished, this, &Worker::reportFinished);
}
void start()
{
qDebug() << "Start" << name;
qThread.start();
}
void reportFinished()
{
qDebug() << "Exit" << name;
}
};
// main application
int main(int argc, char **argv)
{
qDebug() << "Qt Version:" << QT_VERSION_STR;
QCoreApplication app(argc, argv);
Worker worker1("worker 1");
Worker worker2("worker 2");
// install signal handlers
QObject::connect(&worker1.qThread, &QThread::started, &worker2, &Worker::start);
worker1.start();
// runtime loop
return app.exec();
}
输出:
Qt Version: 5.13.0
Start "worker 1"
这是OP观察到的。所以呢?
worker1.qThread.started
信号连接到worker2.start
插槽worker1
已启动worker2
似乎没有开始。 moveToThread()
。
Worker
对象与其成员
QThread
相关联。
QThread
之前有可能吗?
moveToThread()
:
Worker(const QString &name): name(name)
{
//moveToThread(&qThread);
connect(&qThread, &QThread::finished, this, &Worker::reportFinished);
}
输出:
Qt Version: 5.13.0
Start "worker 1"
Start "worker 2"
我之所以评论
moveToThread()
的原因:
qThread::start()
的调用应在主应用程序(线程)的上下文中进行。
worker2
移至其
QThread
意味着该信号已发送到
worker2.qThread
的事件循环-实际上尚未启动。
moveToThread()
应该稍后执行-例如在
started()
信号的 react 中:
#include <QtWidgets>
struct Worker: QObject {
QString name;
QThread qThread;
Worker(const QString &name): name(name)
{
connect(&qThread, &QThread::started, this, &Worker::moveThisToThread);
connect(&qThread, &QThread::finished, this, &Worker::reportFinished);
}
void start()
{
qDebug() << "Start" << name;
qThread.start();
}
void moveThisToThread()
{
moveToThread(&qThread);
qDebug() << name << "associated to its thread, from now.";
}
void reportFinished()
{
qDebug() << "Exit" << name;
}
};
// main application
int main(int argc, char **argv)
{
qDebug() << "Qt Version:" << QT_VERSION_STR;
QCoreApplication app(argc, argv);
Worker worker1("worker 1");
Worker worker2("worker 2");
// install signal handlers
QObject::connect(&worker1.qThread, &QThread::started, &worker2, &Worker::start);
worker1.start();
// runtime loop
return app.exec();
}
输出:
Qt Version: 5.13.0
Start "worker 1"
"worker 1" associated to its thread, from now.
Start "worker 2"
"worker 2" associated to its thread, from now.
So does that mean "QThread::start" is useless as receiver of a signal?
SLOT
来将它们用于信号,过去可能会找到一个更明显的答案:
QThread::start
插槽直接连接到
QThread::started
信号。 (然后
QThread::start
中唯一的一个参数的默认值生效。)
QObject::connect(&worker1.qThread, SIGNAL(started()), &worker2.qThread, SLOT(start()));
输出:
Qt Version: 5.13.0
Start "worker 1"
"worker 1" associated to its thread, from now.
"worker 2" associated to its thread, from now.
由于
Start "worker 2"
现在直接调用
worker1.started()
,因此不再发出
worker2.qThread.start()
。
moveToThread()
问题(也可能)并没有使它令人满意地工作。
关于c++ - 为什么我不能在信号上启动QThread?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63067960/
我是一名优秀的程序员,十分优秀!