PDA

View Full Version : C++11-signal-slot-syntax, use member function "pointers" as function arguments



Maxubuntu23
25th October 2013, 21:51
Greetings, everyone!

I've got a class that represents kind of a tool bar. To add an action, I've managed to provide the following two methods:


QAction* addAction(QString title, QObject* receiver, char* slot);
QAction* addAction(QString title, std::function<void()> functor);

These methods allow me to do things like these:


theToolBar->addAction("foo", theReceiver, SLOT(theSlot()));
theToolBar->addAction("bar", [](){ bar(); })

Now one thing I did not accomplish: Use the new cool C++11-signal-slot-syntax!

I'd like to write my code like this:


theToolBar->addAction("foobar", theReceiver, &TheReceiverClass::theSlot)

But how would the signature of the new overload of addAction look like?

I would appreciate any help. (Quite honestly, this wouldn't be an important fetaure at all but at least it would be consistent.)

Maximilian

amleto
26th October 2013, 01:36
Given that the method signature is e.g.,


void theSlot(int);
then the type is,

void (TheReceiverClass::*)(int)

I suggest you typedef this like,

void (TheReceiverClass::*MyTypeDef)(int);

making your addAction look like,

addAction(string, QObject*, MyTypeDef);

wysota
26th October 2013, 06:40
It's a Qt5 syntax, not a C++11 one. The only thing C++11 introduces in this regard is lambda expressions.