button action clicked() called several times per click?
-
i have my button action connector like this:
void QtDialog::buttonConnect(DialogItemIndex itemS) { QPushButton *buttonP(i_dlgP->QtGetItem<QPushButton>(itemS)); if (buttonP) { QObject::connect(buttonP, &QPushButton::clicked, [this, itemS]() { this->buttonClicked(itemS); }); } }
Problem is, my
buttonClicked()
method is called several times after just one click. what's going on? -
@SGaist said in button action clicked() called several times per click?:
Each time you call buttonConnect with the same value of itemS you connect again the same button, hence your lambda will called as many times as you called buttonConnect for a given itemS.
OMG
-
Hi,
Each time you call
buttonConnect
with the same value of itemS you connect again the same button, hence your lambda will called as many times as you called buttonConnect for a given itemS. -
@SGaist said in button action clicked() called several times per click?:
Each time you call buttonConnect with the same value of itemS you connect again the same button, hence your lambda will called as many times as you called buttonConnect for a given itemS.
OMG
-
well the next obvious question is: can i ask if a button already has a connection?
i see this but that makes me nervous
-
The fast and easy way is to use
Qt::UniqueConnection
as connection type. BUT it won't work with lambdas. So you have to use a private slot.The longest is to rethink the design of buttonConnect. Do you really need the method ? What design is behind it ?
-
i'm porting a monolithic app that handles clicks in a single function that takes an "itemID", so it's prohibitive to retool THAT part for now.
the dialog user may swtich between tabs a dozen times, and each time, the current panel is set up. i'd like the buttonConnect function to ignore subsequent setups. also i somehow just need to get the itemID to the receiver of the signal. How could i do that given the constraints?
i suppose i could buttonP->setProperty("initted", true), then skip out if it's ALREADY set? <--- this worked
-
Why not make a loop that you run once that will connect the button of all the tabs ?
-
well, it's a good idea and i've considered it, but that's a rearchitect of the monolithic app. a major goal is it must still compile and work under the old architecture during the transition, so i can't change that code.
-
I'm not sure I'm following you with regard to that old architecture build.
In any case, if you really have to keep it like that, then as you already had the idea, use a dynamic property and check it before doing the connection.
-
i just mean that the old app must compile as is, and only the lowest level platform specific UI calls are being replaced with QT things. all the independent code must stay the same. to implement as you suggested, i'd have to change the independent code to accommodate QT, and i can't do that.
in any case, setProperty() does the trick.
thanks!