Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it OK to chain signals and slots several times?

I have something like the following design of classes and I'm wondering if it's OK that I use many signal slot connections to communicate between them. There is a MainWindow class which holds all the GUI, then a Wrapper class, which provides an interface to the back-end, then there is a Controller class for spawning and managing threads and finally there are Workers which do all the work.

Now let's say I'm loading a file and want to display progress using a progress bar in the MainWindow. My Worker class sends updateProgress(int progress) to Controller::handleProgress(int progress) slot which again sends progress signal to the Wrapper class, which in return sends a progress signal to the main window, which finally updates the progress bar.

Similarly when the data has been loaded it is processed in the Wrapper class and, again, communicated through signals and slots (although with one less step).

Is it a standard way of doing things in Qt or should I rethink my design?

like image 373
jaho Avatar asked May 02 '14 11:05

jaho


People also ask

Are signals and slots thread safe?

It is generally unsafe to provide slots in your QThread subclass, unless you protect the member variables with a mutex. On the other hand, you can safely emit signals from your QThread::run() implementation, because signal emission is thread-safe.

Can we connect one signal with multiple slots?

If several slots are connected to one signal, the slots will be executed one after the other, in the order they have been connected, when the signal is emitted. Signals are automatically generated by the moc and must not be implemented in the . cpp file. They can never have return types (i.e.

How Qt signals and slots work?

In Qt, we have an alternative to the callback technique: We use signals and slots. A signal is emitted when a particular event occurs. Qt's widgets have many predefined signals, but we can always subclass widgets to add our own signals to them. A slot is a function that is called in response to a particular signal.

How do I turn off QT signal?

Use Once::connect as you would use QObject::connect . If you need to disconnect the slot/functor/signal, disconnect the returned QMetaObject::Connection with static bool QObject::disconnect(const QMetaObject::Connection &connection) .


1 Answers

It is a valid possibility.

Note that Qt allows you to go even further and do the following:

QObject::connect(&sender, SIGNAL(mySenderSignal(int)),
                 &receiver, SIGNAL(myReceiverSignal(int));

That's right, you can bind a signal to a signal. All it does can be seen as "when the sender's signal is emitted, emit the receiver's signal".

If you don't have any specific task to perform in your intermediary slots, that might save you a few lines, while showing exactly what you're doing: you're "forwarding" a signal. Otherwise, if you must absolutely do work in between, then you must keep the signals and the slots.

Note that you still have to be sure that the signals signatures match.

like image 132
JBL Avatar answered Oct 01 '22 04:10

JBL