2013-10-05 29 views
0

我有一个(有点)简单的程序创建新线程,每个连接到插座一个:创建的QObject:findChildren和QThread的

void TelnetServer::incomingConnection(qintptr socketDescriptor) 
{ 
    TelnetConnection *thread = new TelnetConnection(socketDescriptor); 
    connect(thread, SIGNAL(shutdownRequested()), m_controller, SLOT(shutdown())); 
    connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater())); 
    thread->start(); 
} 

一个新的线程后,我输出到qDebug所有列表创建该QThreads(TelnetConnection的)家长像这样的孩子:

QList<QObject*> activeTelnetConnections = m_telnetserver->findChildren <QObject *>(); // Find all QThreads that children of telnetserver 
qDebug() << "Children: " << activeTelnetConnections; 

由于我QThreads自QObject decend,我希望看到QThreads多的列表。但是,我找不到Q线程!这是我看到的:

Children: (QNativeSocketEngine(0x7eb880) , QSocketNotifier(0x7ea5f0)) 

为什么我看不到子线程?这是否意味着线程不再与父对象关联?或者我在这里做错了什么...

回答

1

这是否意味着线程不再与父对象关联?

它可能从未关联。当你构造线程时,你需要传递父对象,但是你的TelnetConnection似乎是错误的,因为它不期望一个父参数,或者你不会传递那个内部通过以下构造函数进一步传递给基类的对象。

QThread(QObject * parent = 0) 

或者您必须稍后调用setParent()。

void QObject::setParent(QObject * parent) 

这将意味着thread.setParent(this);为你的代码,但我宁愿建议修复你的线程类的构造函数或调用它。

或者,您也可以为TelnetConnection明确设置孩子,但如果可能的话,我会建议适当的构造。

+0

就是这样 - 我必须在构造函数的初始化行上设置父级,然后才可见! – TSG