tags:

views:

69

answers:

1

Hello

I have created Qt tree control( and its nodes ) in different thread than the main thread. In the main thread I want to show context menu for the clicked node, so I am connectiong the actions in the menu with appropriate slots in the main thread. The connect function returns true , but slot is never executed. If I explicitly say in connect function that this is Qt :: DirectConnection then everything works fine. Why is this ?

I I create my tree in main thread, everything also works fine , without having to say that this is Qt::DirectConnection .

+4  A: 

See the documentation here.

The default connection type, Qt::AutoConnection, is the same as Qt::DirectConnection if the signal is sent from the same thread as the receiver slot, otherwise the behaviour is the same as Qt::QueuedConnection.

In the case where you create the widget in the main thread, you basically get the same behaviour as when you explicitly specify Qt::DirectConnection.

The behaviour of Qt::QueuedConnection is to call the slot when that threads event loop regains control.

To solve your problem, make sure you have an event loop in every thread which may be receiving signals, unless you manually specify Qt::DirectConnection (which, I assume, will mean the slot is called from the same thread as the signals emitter - basically the equivelent of a normal function call).

Dan
Ok I think I get it now. Because I was emiting the signal from the main thread, and the tree control was created in other thread, I was getting QueuedConnection by default. So, to get the slot to be executed in main thread ( in the same thread where I am emitting ) I had to specify Qt::DirectConnection