OCyril
OCyril

Reputation: 3115

Qt: connecting signal to slot having more arguments

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?

Upvotes: 5

Views: 10247

Answers (4)

edwinc
edwinc

Reputation: 1675

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.

Upvotes: 3

Bood
Bood

Reputation: 193

In some cases, default arguments may help, e.g. declare desiredSlot as:

desiredSlot(int a=0, int b=0)

You cannot access members in default argument though.

Upvotes: 1

DeyyyFF
DeyyyFF

Reputation: 953

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")

Upvotes: 7

Andrea Bergia
Andrea Bergia

Reputation: 5552

That is not the way to connect signals and slots in QT. You should use:

connect(button, SIGNAL(clicked()), receiver, SLOT(slotToBeCalled());

Have a look at the QT documentation.

Upvotes: -1

Related Questions