Results 1 to 7 of 7

Thread: Toggle text for context menu.

  1. #1
    Join Date
    Oct 2007
    Posts
    201
    Thanks
    19
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Toggle text for context menu.

    Hi All,
    I need toggle text for context menu action.E.g , If i've tow action for context menu , "Open", "Close". If i right click it should show Open and if i choose Open and second time if i right click toggle action Close should display.
    Can any body guide me, how to achive this.?

    Thank u all.
    Cheers,
    Phillip



    --- Please post the solution you got to solve your problem. It may help others.

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Toggle text for context menu.

    Set the text in the slot for QAction::trigerred signal.
    something like -
    Qt Code:
    1. void someClass::someSlot()
    2. {
    3. QAction *action = (QAction*)sender();
    4. action->setText( action->text() == "Open" ? "Close" : "Open" );
    5. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Oct 2007
    Posts
    201
    Thanks
    19
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Toggle text for context menu.

    Quote Originally Posted by aamer4yu View Post
    Set the text in the slot for QAction::trigerred signal.
    something like -
    Qt Code:
    1. void someClass::someSlot()
    2. {
    3. QAction *action = (QAction*)sender();
    4. action->setText( action->text() == "Open" ? "Close" : "Open" );
    5. }
    To copy to clipboard, switch view to plain text mode 
    Hi Aamer,
    Thanks for replying to this message. Actually I need to implement toggle action for context menu. But only one text can be added to QAction, like QAction( _icon, _text, _parent ). In my case i need both "Open" and "Close" for the same action. But it should be toggled. Can u tell me how to implement.
    Cheers,
    Phillip



    --- Please post the solution you got to solve your problem. It may help others.

  4. #4
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Toggle text for context menu.

    How are you showing the context menu ? You must be adding some actions to it somewhere ?

  5. #5
    Join Date
    Oct 2007
    Posts
    201
    Thanks
    19
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Toggle text for context menu.

    Quote Originally Posted by aamer4yu View Post
    How are you showing the context menu ? You must be adding some actions to it somewhere ?
    I'm creating like following

    Qt Code:
    1. Action::Action( enum , const QIcon & icon, const QString & text,
    2. QObject * parent, int ) :
    3. QAction( icon, text, parent ),
    4. {
    5. }
    6.  
    7. Action class is derived from QAction.
    8.  
    9. creation of menu action for contextmenu device
    10. --------
    11. createMethode()
    12. {
    13. QMenu * menu = new QMenu( _parent );
    14.  
    15. menu->addAction( new Action( enum value, QIcon(), tr( "Open" ), menu ) );
    16. }
    To copy to clipboard, switch view to plain text mode 
    Cheers,
    Phillip



    --- Please post the solution you got to solve your problem. It may help others.

  6. #6
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Toggle text for context menu.

    Is there a reason to inherit QAction ??
    What I want to know is how you are showing menu. You need to simply follow the following steps -
    Qt Code:
    1. QAction *action = new QAction("Open");
    2. connect(action,trigerred(bool), someClass, mySlot(bool) );
    3. // Add action to any menu u want... when that action is triggered you will reach mySlot function.
    4. // Before I gave hint what to do in mySlot.
    To copy to clipboard, switch view to plain text mode 
    Hope you can now join the pieces together

  7. #7
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Toggle text for context menu.

    or you can just create 2 actions one for open and other for close. Store a flag somewhere which according to the current state of your application and just before you show your menu , hide the appropriate action.

Similar Threads

  1. How to get toggle action text?
    By sudhansu in forum Qt Programming
    Replies: 4
    Last Post: 29th May 2012, 11:57
  2. Context Menu not working
    By waynew in forum Qt Programming
    Replies: 1
    Last Post: 10th January 2010, 03:48
  3. Context menu
    By dejvis in forum Newbie
    Replies: 2
    Last Post: 20th September 2009, 22:02
  4. Qwt and context menu
    By giusepped in forum Qwt
    Replies: 1
    Last Post: 9th December 2008, 08:55
  5. Context Menu
    By RY in forum Newbie
    Replies: 1
    Last Post: 10th September 2008, 07:59

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.