Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Qt - When a slot is called, is the function called a new thread?

I would like to know if the function do_something() is treated as a new thread, when I click on my_button.

connect(my_button, SIGNAL(clicked), this, SLOT(do_something));
like image 636
Béatrice Moissinac Avatar asked Aug 20 '12 05:08

Béatrice Moissinac


1 Answers

The typical signal/slot behavior is determined based on the connection type. When unspecified, it defaults to Qt::AutoConnection and will use the receiver's thread if a direct connection can't be made.

From the docs:

The slot is invoked when control returns to the event loop of the receiver's thread. The slot is executed in the receiver's thread.

You can change the connection type at connect time to alter the behavior:

connect(my_button, SIGNAL(clicked), this, SLOT(do_something),
    Qt::QueuedConnection); // always queue

Since you're talking about a button that's emitting the signal, the default connection type of Qt::AutoConnection implies that a direct connection is made and that the do_something slot will be executed immediately as if it had been called directly at the point the button was clicked.

like image 109
Kaleb Pederson Avatar answered Sep 20 '22 14:09

Kaleb Pederson