Results 1 to 3 of 3

Thread: Problem to recover an object via a QAction

  1. #1
    Join Date
    Apr 2008
    Location
    Fons Outre-Gardons
    Posts
    16
    Thanks
    2
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Problem to recover an object via a QAction

    Hello,

    I have several QPushButtons on which I added the same contextual menu this way :
    Qt Code:
    1. setContextMenuPolicy(Qt::ActionsContextMenu);
    2. QAction* MyAction = new QAction("DoThis");
    3. connect(MyAction, SIGNAL(triggered()), this, SLOT(MySlot()));
    4. addAction(MyAction);
    To copy to clipboard, switch view to plain text mode 

    For every button, there's a contextual menu with the same action. This action is connected to one slot. This slot has to behave differently regarding to the sender.

    So far I can easily recover the occurrence of the button the user clicked on using :
    Qt Code:
    1. QPushButton* PB = qobject_cast<QPushButton*>( sender() );
    2. PB->etc...
    To copy to clipboard, switch view to plain text mode 

    My problem is to recover the occurence of the button on which the user used the contextual menu. For QAction inherits QObject, I tried
    Qt Code:
    1. QAction* TheAction= qobject_cast<QAction*>( sender() );
    To copy to clipboard, switch view to plain text mode 
    But then how to make the link between the QAction and the QPushButton ?
    I associated the same action to all the buttons. Is it a problem ?

    Please feel free to ask me if I didn't made myself clear.
    Thanks in advance.

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem to recover an object via a QAction

    How about QAction::setData?
    You can set some specific data (an enum perhaps), when you create and add the action, later you can retrieve it.


    (PS: Personally I do not really like using sender() etc. and prefer using QSignalMapper if necessary.)

    HTH

  3. #3
    Join Date
    Apr 2008
    Location
    Fons Outre-Gardons
    Posts
    16
    Thanks
    2
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem to recover an object via a QAction

    Thanks for your advice caduel, I'm seeing how I can implement it this way...

Similar Threads

  1. uic3 QAction problem
    By batileon in forum Qt Programming
    Replies: 0
    Last Post: 16th June 2008, 10:35
  2. Problem with my object hierarchy
    By rage in forum Qt Programming
    Replies: 1
    Last Post: 10th October 2007, 09:18
  3. Replies: 3
    Last Post: 15th April 2007, 19:16
  4. Can't create an object : initialisation problem ?
    By Nyphel in forum Qt Programming
    Replies: 5
    Last Post: 12th March 2007, 09:07

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.