Question

I have a (somewhat) simple program that creates new threads, one per connection to a socket:

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();
}

after a new thread is created, I output to qDebug a list of all children of the parent that created the QThreads (TelnetConnection) like this:

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

Since my QThreads decend from Qobject, I expect to see a list of QThreads and more. However, I can't find the Qthreads! This is all I see:

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

Why do I not see the child threads? Does this mean the threads are not associated with the parent object anymore? Or am I doing something wrong here...

Was it helpful?

Solution

Does this mean the threads are not associated with the parent object anymore?

It may have never been associated. When you construct the thread, you need to pass the parent to it, however your TelnetConnection seems to be wrong as it does not expect a parent argument, or you do not pass that which internally passes that further to the base class with the following constructor.

QThread(QObject * parent = 0)

or you have to call setParent() on it later.

void QObject::setParent(QObject * parent)

This would mean thread.setParent(this); for your code, but I would rather suggest to fix either your thread class constructor or the invocation of it.

Alternatively, you can also set the child for the TelnetConnection explicit, but I would suggest the proper construction if possible.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top