Search code examples
c++qtqt5qcombobox

Is it possible to call a slot when any of the widgets in a dialog box emits a signal?


I am trying to create a configuration menu box for an application, and have used a QDialog box to display the options that the user can change. This box contains QComboBoxes and QLineEdits, but a lot of them (7 combo boxes and 12 line edits). There is a QPushButton in the bottom called "Apply Changes" that should get enabled only when any property in the box gets changed.

Do I have to link every signal from each widget with a slot to enable the button individually or is there a signal that the QDialog box itself emits when there is a change in its constituent widgets?

Right now I have this:

connect(Combo1,SIGNAL(activated(QString)),this,SLOT(fnEnable(QString)));
connect(Combo2,SIGNAL(activated(QString)),this,SLOT(fnEnable(QString)))

followed by 17 more lines of these connections.

void MyClass::fnEnable(QString)
{
ApplyButton->setEnabled(true); //It is initialised as false
}

I was wondering if there was a shorter way of doing this, maybe (like I mentioned before) a signal emitted by QDialog (I couldn't find one in the documentation)

I know that this does not speed up the program, as only the required connection is called, but it would make any further attempts at making more ambitious dialog boxes easier.


Solution

  • Actually there is no such signal, but one approach is to create a list of QComboBox, and make the connections with a for, for example:

    QList <*QCombobox> l; 
    
    l<<combobox1<< combobox2<< ....; 
    
    for (auto combo: l) {
        connect(combo, &QComboBox::activated, this, &MyClass::fnEnable);
    }
    

    The same would be done with QLineEdit.