Combine multiple widgets into one of Qt

I QListWidget couple of QComboBox and QListWidget in the project. Their interaction is strongly related - when an element is selected in the combo box, the list is filtered in some way. I copy all the signal and slot connections between these two widgets in several variations of the dialog box, which I think is not a good idea.

Is it possible to create your own widget that will contain these two widgets and will have all the connection to the signal and slot in one place? Something like the following:

 class CustomWidget { QComboBox combo; QListWidget list; ... }; 

I want to use this widget as one widget.

+5
source share
1 answer

The usual way to do this is by subclassing QWidget (or QFrame ).

 class CustomWidget: public QWidget { Q_OBJECT CustomWidget(QWidget *parent) : QWidget(parent) { combo = new QComboBox(...); list = new QListWidget(...); // create the appropriate layout // add the widgets to it setLayout(layout); } private: QComboBox *combo; QListWidget *list; }; 

Manage all the interactions between the list and combos in this custom widget (by connecting the appropriate signals to the corresponding slots, possibly to define your own slots).

Then you expose your user view / widget API through the selected signals and slots, possibly simulating them in a list and / or combo.

In the Address Book tutorial , you will get to know all of this, including creating a custom widget and defining signals and slots for it.

+11
source

Source: https://habr.com/ru/post/922356/


All Articles