PDA

View Full Version : How emit signal when a QCheckBox inserted in a QList<QCheckBox*> change its status?



andreaQt
1st October 2020, 16:58
I wrote the code attacched & it work (in editobject.cpp find the text "QUESTION:" and read my comment) but i want to know if is it possible with just a connect signal/call (i make my apologise for my bad english) to have the discrimination of what QCheckBox inserted in a QList<QCheckBox*> change its status?

Lesiok
1st October 2020, 18:11
Read about QObject::sender().

andreaQt
2nd October 2020, 10:18
please more info

d_stranz
2nd October 2020, 17:16
QObject::sender() gives you the pointer to the QObject (in your case the QCheckBox) instance that emitted the signal that the slot is handling. So if you have multiple check boxes connected to the same slot, you can use sender() to determine which checkbox sent the signal.



// Your member variable that stores your checkboxes
QList<QCheckbox *> myListOfCheckboxes;

// Slot connected to QCheckBox::toggled() signal
void MyWidget::onCheckboxToggled( bool bChecked )
{
int whichBox = myListOfCheckboxes.indexOf( sender() );
}

andreaQt
2nd October 2020, 18:42
OK but i have no idea about to write (to catch the signal):
connect (list_check_box, ????, ...., ????);
or how the slot is called?

and when i try to implement in my code:


void EditObject::onCheckboxToggled(bool bChecked)
{
int whichBox = list_check_box->indexOf(sender());
}

i have this error -> error: reference to type 'QCheckBox *const' could not bind to an rvalue of type 'QObject *'

d_stranz
2nd October 2020, 20:19
for ( auto index; index < numberOfCheckBoxes; ++index )
connect( list_check_box[ index ], &QCheckBox::toggled, this, &MyWidget::onCheckboxToggled );


Of course, you can't call connect() until after you have created all of your checkboxes and put them into the QList.



QCheckBox * pCB = qobject_cast< QCheckBox * >( sender() );
int whichBox = list_check_box.indexOf( pCB );


list_check_box should be defined as "QList< QCheckBox *>" so you use "." to call methods, not "->". There is no reason to define list_check_box as a "QList<QCheckBox *> *".