Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt: connecting signal to slot having more arguments

Tags:

c++

signals

qt

I want to connect a signal clicked() from the button to a slot of different object.

Currently I connect signal to helper method and call desired slot from there:

connect(button, SIGNAL(clicked()), this, SLOT(buttonClicked()));

void buttonClicked() { // Helper method. I'd like to avoid it.
    someObject.desiredSlot(localFunc1(), localFunc2());
}

But maybe there is a more simple and obvious way to do this?

like image 742
OCyril Avatar asked Dec 28 '22 15:12

OCyril


2 Answers

is this what you want to do:

the signal clicked should be connected to the "desiredSlot" which takes two arguments that are returned by localFunc1 & 2 ??

this is not possible, as you can read in the QT docs. A slot can take less arguments than provided by the signal - but not the opposite way! (The documentation says "This connection will report a runtime error")

like image 100
DeyyyFF Avatar answered Jan 10 '23 05:01

DeyyyFF


This ought to work with the new signal/slot mechanism in qt 5:

connect( button, &QPushButton::clicked, [&](){ someObject.desiredSlot( localFunc1(), localFunc2() ); } );

You will need to adjust the lambda capture to your needs.

like image 29
edwinc Avatar answered Jan 10 '23 06:01

edwinc