in Qt Designer, how to create signal/slot from QPushButton that passes buttonID?
-
wrote on 8 May 2019, 16:48 last edited by
-
wrote on 24 May 2019, 04:20 last edited by
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
-
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?
wrote on 8 May 2019, 16:57 last edited by@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
-
wrote on 8 May 2019, 17:17 last edited byThis post is deleted!
-
wrote on 22 May 2019, 15:21 last edited by davecotter
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); }
-
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]() { ... }); ...
-
wrote on 23 May 2019, 11:24 last edited by
why shouldn't i use sender() ?
-
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 -
wrote on 24 May 2019, 04:20 last edited by
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