Question

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?

Was it helpful?

Solution

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.

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