user15202364
user15202364

Reputation:

Connecting a function with a slot in Qt5

As in Qt5, there's a new signal-slot syntax and I tried utilizing it.

Firstly, I created a void return type function:

void update()
{
    cerr << "complete";
}

And connected it to &QRadioButton::pressed

QRadioButton *button = new QRadioButton;
QObject::connect(button, &QRadioButton::pressed, update);

Things went fine. Afterward, I tried inserting a parameter:

void update(bool trigger)
{
    if (!trigger) {return;}
    cerr << "complete";
}

And tried connecting:

QRadioButton *button = new QRadioButton;
QObject::connect(button, &QRadioButton::toggled(bool), update);

But it returned error: called to non-static member function without an object argument.

I tried this as well:

QObject::connect(button, &QRadioButton::toggled(bool), update(bool));

with the same result.

So, how can I patch this? Or, more favorable, how can I implement my function as a slot and get Qt to do the job the old fashion way with SIGNAL() and SLOT()?

Sidenote: I fully understood and also tested the first case (with void return type), but I want to know if I ever have to deal with the second case (which will be a lot, actually), what would be a good option.

Upvotes: 0

Views: 414

Answers (1)

eyllanesc
eyllanesc

Reputation: 244003

You don't have to set the signature since Qt will use the signature of the slot so your initial code should work:

QObject::connect(button, &QRadioButton::pressed, update);

Upvotes: 1

Related Questions