Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a signal as function parameter?

Tags:

c++

qt

So i am looking to make our own generic inherited checkbox class that will be able to take in some values in its constructor and pop out a widget that is fully connected to our model in the manner we need.

Currently we do something like this within our view

connect(checkboxWidget, &QCheckbox::Clicked, this, &VMyView::Signal);

Which emits the Signal from VMyView when the checkbox is clicked.

If i wanted to pass that signal as a parameter into my new inherited class to be hooked up in its own connect statement, how would I do so?

Research has shown me i can pass a const char* but i get compilation errors that the signal/slot do not match.

Example

CheckBox(View myView, const char* signal)
{
    connect(this, &QCheckBox::Clicked, myView, signal);
}

Returns an error that Signal and slot arguments are not compatible. Ive also tried SIGNAL(signal) with the same result.

like image 556
user1024792 Avatar asked Oct 29 '22 23:10

user1024792


1 Answers

The solution ended up being fairly simple in the end

Instead of using this from within my View

connect(pCheckbox, &QCheckBox::clicked, this, &MyView::Signal);

I use

    connect(this, &QCheckBox::clicked, View, signal);

Where signal and comes into my function via a function pointer

MyCheckBox::MyCheckBox(QWidget* parent, MyView* View, void(MyView::*signal)(bool))

The key takeaway is

void(MyView::*signal)(bool) 

is equal too

&MyView::Signal
like image 199
user1024792 Avatar answered Nov 15 '22 06:11

user1024792