in Qt Designer, how to create signal/slot from QPushButton that passes buttonID?
-
see below:
Instead of having each button call a separate function, can i have them all call the same function but pass an ID into that function, eg:clicked_button(int buttonID)
and can i do this in Designer, not in code?
-
i'm convinced. i was hoping to wire the signal / slots in Qt Designer, but you're right it's better to just do it programmatically. new code:
void SetUpButtons() { QPushButton *buttonP; DialogItemIndex itemS; loop (3) { itemS = _index + 1; buttonP = i_dlgP->QtGetItem<QPushButton>(itemS); connect(buttonP, &QPushButton::clicked, [this, itemS]() { this->buttonClicked(itemS); }); } } void QQuick3::buttonClicked(DialogItemIndex itemS) { done(itemS); }
:D
-
@davecotter said in in Qt Designer, how to create signal/slot from QPushButton that passes buttonID?:
can i have them all call the same function but pass an ID into that function
Yes.
QSignalMapper
orstd::bind
and can i do this in Designer, not in code?
No
-
This post is deleted!
-
i was able to create something that worked for my purposes: you CAN have every button go to one function, then ask for the SENDER of the signal, and get the button that sent it, then case on that button. eg:
void QQuick3::handleButton() { QPushButton *buttonP(qobject_cast<QPushButton *>(QObject::sender())); DialogItemIndex itemS(i_dlgP->QtNameToItem(buttonP->objectName())); done(itemS); }
-
@davecotter You can do that but it isn't a clean solution (I guess that's why @VRonin didn't suggest this "solution"). You should avoid using sender().
Since C++11 you can use lambdas:int id = i_dlgP->QtNameToItem(button1->objectName()); connect(button1, &QPushButton::clicked, [this, id]() { ... }); id = i_dlgP->QtNameToItem(button2->objectName()); connect(button2, &QPushButton::clicked, [this, id]() { ... }); ...
-
why shouldn't i use sender() ?
-
@davecotter Because you tightly couple sender and receiver and that is bad design. The receiver should not know anything about the sender, and sender about the receiver. With your sender() implementation your slot now have to know how to get the id. If that logic changes you will have to change the slot as well.
sender() is really just a work around.
See also https://doc.qt.io/qt-5/qobject.html#sender -
i'm convinced. i was hoping to wire the signal / slots in Qt Designer, but you're right it's better to just do it programmatically. new code:
void SetUpButtons() { QPushButton *buttonP; DialogItemIndex itemS; loop (3) { itemS = _index + 1; buttonP = i_dlgP->QtGetItem<QPushButton>(itemS); connect(buttonP, &QPushButton::clicked, [this, itemS]() { this->buttonClicked(itemS); }); } } void QQuick3::buttonClicked(DialogItemIndex itemS) { done(itemS); }
:D