Rich von Lehe
Rich von Lehe

Reputation: 1542

Qt/OO best practices: connecting signals and slots

I'm relatively light on Qt experience, though I definitely like it. One thing I'm uncertain of is where the best place is to connect signals to slots. Here's an example from my small device with a touchscreen:

I have a class called RadioModel that is owned by my QApplication. My QApplication also owns what I call a ViewController. The ViewController owns the view - i.e. all of the widgets that make up the user interface. There is some hierarchy to the UI widgets, of course. The top-level widget is a QHBoxLayout, which has indicator labels at the top, and a QTabWidget at the bottom. The QTabWidget has 3 screens, each with things like QLabels, QGroupBoxes, QComboBoxes, etc.

The model needs to be signaled when values in the QGroupBoxes and QComboBoxes change. My initial thought was to have a chain like this:

QRadioButton (part of QGroupBox) signals clicked(), this goes to SLOT of the current tab of the QTabWidget, which looks at the sender to determine the value (which radio button) that was clicked, then emits its own signal like RadioChanged. This RadioChanged signal would get connected to the ViewController's RadioChanged signal, which would in turn be connected to the Model's UpdateRadio slot.

Generally speaking, when a widget that is fairly isolated from the model emits a signal of interest, is it fair to have this long chain of signals and slots to get that value change back to the model? Would it be better to pass the model into the ViewController and possibly some of its objects so that the signals can be connected with a shorter path?

Thanks - hope that was understandable and hopefully not too subjective..

Upvotes: 1

Views: 1015

Answers (1)

Jay
Jay

Reputation: 14441

I implement mine as 'the controller controls the flow of data'. It gets signals from the view. It then updates the model as needed. The view knows nothing about the model and vice versa.

I notice you said you're signaling each user input change. This can be very inefficient and makes it difficult to cancel half made changes. I usually let the user change all the fields and only update the model when the OK button is pressed.

Upvotes: 2

Related Questions