【发布时间】:2013-10-10 15:30:52
【问题描述】:
为了减轻我的清理工作,我想将我的工作对象的父对象设置为它被移动到的 Qthread。 (见下文)。
void TelnetServer::incomingConnection(qintptr socketDescriptor)
{
QThread * TelnetConnectionThread = new QThread(this);
TelnetConnection *worker = new TelnetConnection(socketDescriptor,TelnetConnectionThread);
connect(TelnetConnectionThread, SIGNAL(started()), worker, SLOT(start()));
connect(TelnetConnectionThread, SIGNAL(finished()), worker, SLOT(deleteLater()));
worker->moveToThread(TelnetConnectionThread); // Move worker into QThread
TelnetConnectionThread->start();
}
在 start() 行之前,我添加了:
worker->setParent(TelnetConnectionThread);
但在运行时我看到一个错误,我不能这样做,因为新的父级在不同的线程中。怎么可能?在上面的行中,我将工作线程移到了新线程……所以工作线程应该与 TelnetConnectionThread 在同一个线程中。帮忙?
我通过一些 qDebug's 和 thread() 确认工作线程确实被转移到了新线程!
【问题讨论】:
-
你应该在连接插槽之前移动到线程,它确保连接是正确的类型
-
@ratchetfreak 怎么样?连接类型在信号发出时确定。
-
@thuga 我的错我的印象是 AutoConnection 在连接时被转换为直接或队列
-
@ratchetfreak 是的,我也这么认为了很长时间。我不确定我从哪里得到这些信息,但我确定我在某个地方读到过它。但后来我注意到在 Qt 文档中它说:
The type of connection is determined when the signal is emitted..
标签: c++ multithreading qt move qobject