Results 1 to 2 of 2

Thread: Context menu / connect with parameters

  1. #1
    Join Date
    Feb 2009
    Location
    England & Italy
    Posts
    1
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Context menu / connect with parameters

    I have a problem with context menus. I would like to create one in a dynamic way, i.e. passing a set of string variables:

    Qt Code:
    1. void MyClass::contextMenu(std::vector<QAction*>& labelsAct)
    2. {
    3. for(size_t i = 0; i < labelsAct.size(); ++i)
    4. {
    5. QObject::connect(labelsAct[i], SIGNAL(mySignal(int)),
    6. this, SLOT(doSomething(int)));
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    This is the message I get at runtime:

    Qt Code:
    1. Object::connect: No such signal QAction::mySignal(int)
    To copy to clipboard, switch view to plain text mode 

    doSomething(int) is defined in the MyClass class.
    mySignal(int) is declared in the MyClass class; why does it look for the signal in QAction?

    If I remove the int parameters from those function (definitions), the connect works correctly. But I need those parameters.

    Thank you...
    Last edited by pietrom; 18th February 2009 at 17:54.

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Context menu / connect with parameters

    QAction doesn't has such signal
    try to do next
    Qt Code:
    1. ...
    2. connect(this, SIGNAL(mySignal(int)), SLOT(doSomething(int)));
    3. ...
    4.  
    5. void MyClass::contextMenu(std::vector<QAction*>& labelsAct)
    6. {
    7. for(size_t i = 0; i < labelsAct.size(); ++i)
    8. {
    9. QAction *action = labelsAct[i];
    10. action->setProperty("myValue", i);
    11. QObject::connect(action, SIGNAL(triggered()),
    12. this, SLOT(mySlot()));
    13. }
    14. }
    15.  
    16. void MyClass::mySlot()
    17. {
    18. QAction *action = qobject_cast<QAction *>(sender());
    19. if (!action)
    20. return;
    21. int myValue = action->property("myValue").toInt();
    22. emit mySignal(myValue);
    23. }
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. The following user says thank you to spirit for this useful post:

    pietrom (19th February 2009)

Similar Threads

  1. Context Menu on QTableWidget
    By ankurjain in forum Qt Programming
    Replies: 9
    Last Post: 17th December 2009, 09:52
  2. Shortcut key for context menu
    By darshan.hardas in forum Qt Programming
    Replies: 1
    Last Post: 28th December 2008, 20:32
  3. QItemDelegate Context Menu
    By aekilic in forum Qt Programming
    Replies: 16
    Last Post: 3rd December 2008, 09:29
  4. context menu problem
    By dreamer in forum Qt Programming
    Replies: 1
    Last Post: 25th May 2008, 13:18
  5. Replies: 4
    Last Post: 25th June 2007, 20:40

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.