Results 1 to 3 of 3

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

  1. #1
    Join Date
    May 2010
    Location
    Berlin
    Posts
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Question C++11-signal-slot-syntax, use member function "pointers" as function arguments

    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:

    Qt Code:
    1. QAction* addAction(QString title, QObject* receiver, char* slot);
    2. QAction* addAction(QString title, std::function<void()> functor);
    To copy to clipboard, switch view to plain text mode 

    These methods allow me to do things like these:

    Qt Code:
    1. theToolBar->addAction("foo", theReceiver, SLOT(theSlot()));
    2. theToolBar->addAction("bar", [](){ bar(); })
    To copy to clipboard, switch view to plain text mode 

    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:

    Qt Code:
    1. theToolBar->addAction("foobar", theReceiver, &TheReceiverClass::theSlot)
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: C++11-signal-slot-syntax, use member function "pointers" as function arguments

    Given that the method signature is e.g.,

    Qt Code:
    1. void theSlot(int);
    To copy to clipboard, switch view to plain text mode 
    then the type is,
    Qt Code:
    1. void (TheReceiverClass::*)(int)
    To copy to clipboard, switch view to plain text mode 

    I suggest you typedef this like,
    Qt Code:
    1. void (TheReceiverClass::*MyTypeDef)(int);
    To copy to clipboard, switch view to plain text mode 

    making your addAction look like,
    Qt Code:
    1. addAction(string, QObject*, MyTypeDef);
    To copy to clipboard, switch view to plain text mode 
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: C++11-signal-slot-syntax, use member function "pointers" as function arguments

    It's a Qt5 syntax, not a C++11 one. The only thing C++11 introduces in this regard is lambda expressions.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 11
    Last Post: 20th February 2013, 16:54
  2. Replies: 0
    Last Post: 6th December 2012, 17:54
  3. Replies: 2
    Last Post: 22nd November 2011, 06:19
  4. Replies: 8
    Last Post: 24th February 2011, 14:22
  5. Eclipse "File->New Class" function question
    By MarkoSan in forum General Programming
    Replies: 0
    Last Post: 20th January 2008, 19:22

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.