how to handle too many similar checkbox(30+ slot functions)?
-
this code is generated by qt designer,
QCheckBox *sC1;
QCheckBox *sC2;
QCheckBox *sC3;
QCheckBox *sC4;
QCheckBox *sC5;
QCheckBox *sC6;
QCheckBox *sC7;
QCheckBox *sC8;
QCheckBox *sC9;
QCheckBox *sC10;...
QCheckBox *sC30;
I need to write slot function to response the check event, since so many similiar checkboxes, it is tedious to write 30 slot functions, so I wanna
-
put this checkbox pointer in a pointer array mySC[30], how to set the qt designer? is it possible?
-
I just want to write one slot function to responce those checkbox envents, in one slot function, i check which box is checked
I know in MFC framework, it provides a solution to deal with this kind things, in QT, is there any method to cope with this situation?
Thanks a lot
-
-
this code is generated by qt designer,
QCheckBox *sC1;
QCheckBox *sC2;
QCheckBox *sC3;
QCheckBox *sC4;
QCheckBox *sC5;
QCheckBox *sC6;
QCheckBox *sC7;
QCheckBox *sC8;
QCheckBox *sC9;
QCheckBox *sC10;...
QCheckBox *sC30;
I need to write slot function to response the check event, since so many similiar checkboxes, it is tedious to write 30 slot functions, so I wanna
-
put this checkbox pointer in a pointer array mySC[30], how to set the qt designer? is it possible?
-
I just want to write one slot function to responce those checkbox envents, in one slot function, i check which box is checked
I know in MFC framework, it provides a solution to deal with this kind things, in QT, is there any method to cope with this situation?
Thanks a lot
@hitbuyi said in how to handle too many similar checkbox(30+ slot functions)?:
put this checkbox pointer in a pointer array mySC[30]
Why?
You can use one slot for all your check boxes.
In the slot you get access to the check box which emitted the signal using sender() method. -
-
this code is generated by qt designer,
QCheckBox *sC1;
QCheckBox *sC2;
QCheckBox *sC3;
QCheckBox *sC4;
QCheckBox *sC5;
QCheckBox *sC6;
QCheckBox *sC7;
QCheckBox *sC8;
QCheckBox *sC9;
QCheckBox *sC10;...
QCheckBox *sC30;
I need to write slot function to response the check event, since so many similiar checkboxes, it is tedious to write 30 slot functions, so I wanna
-
put this checkbox pointer in a pointer array mySC[30], how to set the qt designer? is it possible?
-
I just want to write one slot function to responce those checkbox envents, in one slot function, i check which box is checked
I know in MFC framework, it provides a solution to deal with this kind things, in QT, is there any method to cope with this situation?
Thanks a lot
-
-
@hitbuyi
In addition to @jsulm'ssender()approach (which sometimes has issues, though I am not dissing it here), there are lambdas for passing a parameter or https://doc.qt.io/qt-5/qsignalmapper.html for doing just what you want here.@JonB great, in future I'll try it. my current Qt is 5.9.9
I have figured out a solution, not so perfect, but works in some degree:
I labeled those QCheckBox as label_1,label_2, ..., label_30 in Qt designer, in my progam, I read label text, get int of 1,2,...,30 from label_xx, thus I know which checkbox is selected .void Simulator::on_showRadar() {
QString tag;
QString str;
int idx;
QCheckBox chk = qobject_cast<QCheckBox>(sender());
tag = chk->text();
str = tag.mid(5,6);
idx = str.toInt();
...
}the is method is faster than searching from beginning to end one by one
-
@JonB great, in future I'll try it. my current Qt is 5.9.9
I have figured out a solution, not so perfect, but works in some degree:
I labeled those QCheckBox as label_1,label_2, ..., label_30 in Qt designer, in my progam, I read label text, get int of 1,2,...,30 from label_xx, thus I know which checkbox is selected .void Simulator::on_showRadar() {
QString tag;
QString str;
int idx;
QCheckBox chk = qobject_cast<QCheckBox>(sender());
tag = chk->text();
str = tag.mid(5,6);
idx = str.toInt();
...
}the is method is faster than searching from beginning to end one by one
@hitbuyi said in how to handle too many similar checkbox(30+ slot functions)?:
QCheckBox chk = qobject_cast<QCheckBox>(sender());
Should be
QCheckBox chk = qobject_cast<QCheckBox*>(sender());Also you should check whether chk is nullptr or not before using it.
-
@hitbuyi
In addition to @jsulm'ssender()approach (which sometimes has issues, though I am not dissing it here), there are lambdas for passing a parameter or https://doc.qt.io/qt-5/qsignalmapper.html for doing just what you want here.@JonB I'm dissing it, sender() same as processEvents() should be part of the private api only and not accessible by normal framework users


Btw, before lambdas, there was a thing called https://doc.qt.io/qt-5/qbuttongroup.html
in cases like this one -
@JonB I'm dissing it, sender() same as processEvents() should be part of the private api only and not accessible by normal framework users


Btw, before lambdas, there was a thing called https://doc.qt.io/qt-5/qbuttongroup.html
in cases like this one@J-Hilk
I don't diss @jsulm because he knows more than I :)My reservation about
sender()was that as I understand it (not tested) it doesn't work if the connection is a lambda, as well as your case.QButtonGroupis possible here, but it's a bit, well, button-y!QSignalMapperis much broader.@hitbuyi
If you're going to use your way of a "tag", instead of parsing it out of the button text consider usingsetObjectName()which is applicable to allQObjects. That can be set in Designer too. It has a number of advantages over the text of a button as an identifier.