views:

34

answers:

2

I have a Qt application that has two threads: the main thread that handles the GUI and a second thread that manages network connections. Here is the thread code:

void thread::run()
{
    QTcpServer server;

    server.connect(&server,SIGNAL(newConnection()),this,SLOT(OnConnect()));

    //...
}

When I put a breakpoint at the start of OnConnect() and debug the application, it announces that OnConnect() is being called from the main thread!

How can I have OnConnect() run in the same thread as the QTcpServer?

A: 

It seems like the problem was that I wasn't passing Qt::DirectConnection as the last parameter of connect().

After adding that, it worked.

George Edison
+3  A: 

To give a more thorough answer, look a little deeper into how signal-slot connections and thread contexts interact. Basically, for more connections (auto-connect), the slot will be directly called if both the emitter and the receiver are in the same thread context, otherwise it will be a queued connection, and the slot will be run in the thread context of the object that contains the slot. In this case, it must be queued, which implies that your thread is part of the main application's thread context, not its own. This is reinforced by the documentation Qt provides for an overview of its threading, where it states that the QThread instance is "owned" by the thread context that created it, not the thread context that it represents. This means you have three main choices:

  1. You can use moveToThread() to move the thread into its own context. Note that this may cause problems when deleting the thread unless you move it back to the context where it will be destroyed, and this can only be done in the source-thread context, so it would have to be done before the run function exited.
  2. You can treat the QThread instance as a handle to the thread, not as being part of the thread itself. If you need things done in the context of the new thread, create a different object to handle those, and instantiate them in the context of the new thread (inside the run function). This is what I would recommend.
  3. Force a direct connection. This means you would need to ensure the code running in the slot is thread-safe, ignoring Qt's built-in methods of making those functions thread-safe. This is what you have done.
Caleb Huitt - cjhuitt