Qt Slot Pointer
I want to create a pointer to a Qt Slot:
union {
void (*set_slot)(unsigned long value);
void (*refresh_slot)(void);
} the_slot;
Slot definition:
void set_pwm(unsigned long new_pwm);
I am trying to do something like this:
the_slot.set_slot = set_pwm;
But the compiler says the signature doesn't match:
error: argument of type
void (DriverBoard::)(long unsigned int)' does not match
void (*) (long unsigned int) '
Hint: the slot is in the DriverBoard class
Any idea where is my mistake?
And if someone knows - is it possible to do this with signals as well?
Thank! Simon
Slots and signals are identified by their names (when you use SLOT(set_pwm(unsigned long))
in your code, you build a string). You can just store the name and object and then call the slot using QMetaObject
.
You can use pointers to member functions in C ++ (see the C ++ faq ), but in this case, I would suggest using Qt's meta-object system.
source to share
Following on from Lukasz Lalinsky's answers, "missed" signals and slots can be as simple as this:
void Foo::bar(const QObject *sender, const QString &signal,
const QObject *receiver, const QString &slot)
{
// ...
connect(sender, signal, receiver, slot);
// ...
}
// ...
fooObject->bar(aSender, SIGNAL(aSenderSignal(const QString &)),
aReceiver, SLOT(aReceiverSlot(const QString &)));
// ...
source to share