PDA

View Full Version : How to declare SLOT as a parameter to member function?



QPlace
4th August 2007, 04:41
How can I pass slot as a parameter to class member function and then call "connect"?
Below is the sketch of what I am trying to do. What is the proper way to pass the slot to a member function to be later passed as a parameter to "connect"?

class A
{
private slot:
void DoSmth() {}; // slot declaration
public:
A()
{
Connector(&A::DoSmth); //I am trying to pass the slot as function pointer
}
void Connector(void (A::*p) ()) // what is the right way to declare the function with slot as a parameter?
{
connect (....SLOT(p)); // compiles, but does not work. Apparently I am doing smth. wrong.
}
}

Michiel
4th August 2007, 09:24
In Qt, signals and slots are const char*. You can do it like this, for example:


void connectSlot(const char* slot) {
connect(this, SIGNAL(something(int)), this, slot);
}

connectSlot(SLOT(somethingElse(int)));

Function pointers don't work, because SLOT is a macro that uses the text you place in it.

scottaronbloom
17th July 2018, 00:41
You can also do this using the new Qt connect system by passing function pointers

Qt Code:


void Class:connectSlot( void(Class::* slotName )() )
{
connect( this, &Class::noParamSignal, this, slotName );
}