Question about signals
-
I'm creating a few check boxes in a loop and connecting them to a lambda functon like this:
connect(cbtn, &QCheckBox::clicked, this, [=](){funct(something);});
and I need to know which state the check box is in (checked or not) in the function. Is there a way to find this out with the QObject::sender()?
-
just pass the checkbox as a second argument to
funct
for example, if the signature isvoid MyClass::funct(QString);
change it tovoid MyClass::funct(QString,QCheckBox*);
and chenge the connect toQObject::connect(cbtn, &QCheckBox::clicked, this, std::bind(&MyClass::funct,std::placeholders::_1,cbtn));
orQObject:connect(cbtn, &QCheckBox::clicked, this, [=](){funct(something,cbtn);});
-
Why not
connect(cbtn, &QCheckBox::stateChanged, this, [=](int state){ fnct(something, state); });
https://doc.qt.io/qt-5/qcheckbox.html#stateChanged
In addition you can pass also the sender, like @VRonin wrote here
@VRonin said in Question about signals:
QObject:connect(cbtn, &QCheckBox::clicked, this, [=]{funct(something,cbtn);});
-
Use
QWidget
orQObject
as parameter infnct
instead ofQCheckBox
, then cast it to sender type.@Inzinejkr said in Question about signals:
I'm using the same function for connecting radio buttons and line edits. How would that work?
The whole idea seems weird. What does your function do? Why do you want to use the same connection for
QLineEdit
type widgets and different button types? They have completely different behaviors and functions.
QLineEdit
also has noclicked
signal, unless you have some custom typeQLineEdit
subclasses. -
There are two voters here for choosing to pass the sending object ---
QCheckBox*
or whatever --- as a parameter to the signal/slot. I will just observe that all (at least widget) Qt-supplied signals/slots have chosen not to do this --- although they easily could have --- and instead pass a parameter for, say, the checked state or the line edit's string. Make of that what you will....@Inzinejkr
As @Pl45m4 has said: there may be some commonality in, say, checkbox or radiobutton clicking events, e.g. on/off state. It is harder to see where that would be shared with line edit clicking. Perhaps the line edits would be better with a different slot? -
The function just takes the text() of whatever is clicked (QLineEdit::editingFinished, QCheckBox::clicked, QRadioButton::clicked) and stores it in a database.. But, currently I have no way of knowing whether the user toggled on or off the check box.. I assumed there would be some easy way without editing all the code..
-
use QObject::sender() and try to cast it to a QCheckBox
-
@Inzinejkr
Here is one way, a touch different:connect(checkbox, &QCheckBox::clicked, this, [=](bool checked = false) { if (checked) saveText(checkbox->text());} ); connect(radiobutton, &QRadioButton::clicked, this, [=](bool checked = false) { if (checked) saveText(radiobutton->text());} ); connect(lineedit, &QLineEdit::editingFinished, this, [=]() { saveText(lineEdit->text());} );
There are pros & cons to this approach versus passing the widget through the signal/slot.
-
@Christian-Ehrlicher said in Question about signals:
use QObject::sender() and try to cast it to a QCheckBox
I just get QCheckBox(0x1f8e510)?
@JonB said in Question about signals:
@Inzinejkr
Here is one way, a touch different:connect(checkbox, &QCheckBox::clicked, this, [=](bool checked = false) { if (checked) saveText(checkbox->text());} );
The thing is, I always have to know a state change and what that state is, not just for checked.. Guess I'll just make a new function for the check boxes..
-
@Inzinejkr said in Question about signals:
use QObject::sender() and try to cast it to a QCheckBox
I just get QCheckBox(0x1f8e510)?
And you want to tell me what with this?
You wanted access the the QCheckBox, you have it now - so what's the problem? -
@Inzinejkr said in Question about signals:
@Christian-Ehrlicher said in Question about signals:
use QObject::sender() and try to cast it to a QCheckBox
I just get QCheckBox(0x1f8e510)?
He wants you to go:
QCheckBox *checkbox = qobject_cast<QCheckBox *>(QObject::sender()); if (checkbox != nullptr) { if (checkbox->checked()) saveText(checkbox->text()); }
Read up qobject_cast<>().