In my Qt Application I am dynamically creating 'Questions' in QVBoxLayouts for a 'Questionnaire'. There are 3 types of Questions: Boolean, Text, & Radio.
When the user 'adds a question' to the questionnaire, they are presented with a QComboBox. When the index/text of this QComboBox is edited, I want to act upon the SIGNAL emitted.
I would like to have something like Java's (from an old Android Project):
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//Code to run...
}
});
Is it possible to have the same in Qt/C++ like:
connect(qvectorOfComboBoxes.at(qvectorOfComboBoxes.end()), SIGNAL(currentTextChanged(QString)),
this, SLOT(
void comboBoxTextChanged(QString newComboxBoxText)
{
//This doesn't work
} )) ;
I understand from another post on here the ideal approach is a QSignalMapper, but was hoping to perform the task in a manner similar to above.
Usually, I find my answers either on here or from a related Google search (I am probably searching the wrong thing as I don't know the name for this), and so was hoping somebody here could give me a yay or nay. Thanks
In Qt5 and using a C++11-enabled compiler, you can use lambdas as slots, as explained here:
connect(sender, SIGNAL(signal(QString)), [](QString newComboxBoxText) {
// add your code here
});
Otherwise, you can use sender()
to query the QObject*
which sent the signal, if this is enough information you need. To cast it to a QComboBox*
please use qobject_cast<QComboBox*>
and Q_ASSERT
that it's not null. (You can't get a compile-time error that it was connected to some other type.)