views:

285

answers:

2

Hi,

i want to build a pointer to a Qt Slot:

union {
    void (*set_slot)(unsigned long value);
    void (*refresh_slot)(void);
} the_slot;

The slot definition is:

void set_pwm(unsigned long new_pwm);

I try to do something like this:

the_slot.set_slot = set_pwm;

But the compiler says that the signature does not match:

error: argument of type void (DriverBoard::)(long unsigned int)' does not match void (*)(long unsigned int)'

hint: the slot is in class DriverBoard

Any idea where my error is?

And if someone knows - is it possible to do something like that with signals also?

Thanks! Simon

+6  A: 

Slots and signals are identified by their names (when you do use SLOT(set_pwm(unsigned long)) in your code, you are constructing a string). You can simply store the name and the 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'd suggest to use Qt's meta object system.

Lukáš Lalinský
Thanks! That makes it a lot easier.
Simon
A: 

Following on from Lukáš Lalinský's answer, 'passing' 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 &))); 
  // ...
Sam Dutton
It's better to pass signal/slot names as `const char *`. The conversion to QString is not necessary.
Lukáš Lalinský