PDA

View Full Version : Toggle text for context menu.



phillip_Qt
12th February 2010, 05:04
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.

aamer4yu
12th February 2010, 05:24
Set the text in the slot for QAction::trigerred signal.
something like -

void someClass::someSlot()
{
QAction *action = (QAction*)sender();
action->setText( action->text() == "Open" ? "Close" : "Open" );
}

phillip_Qt
15th February 2010, 04:57
Set the text in the slot for QAction::trigerred signal.
something like -

void someClass::someSlot()
{
QAction *action = (QAction*)sender();
action->setText( action->text() == "Open" ? "Close" : "Open" );
}

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.

aamer4yu
15th February 2010, 07:06
How are you showing the context menu ? You must be adding some actions to it somewhere ?

phillip_Qt
15th February 2010, 08:14
How are you showing the context menu ? You must be adding some actions to it somewhere ?

I'm creating like following


Action::Action( enum , const QIcon & icon, const QString & text,
QObject * parent, int ) :
QAction( icon, text, parent ),
{
}

Action class is derived from QAction.

creation of menu action for contextmenu device
--------
createMethode()
{
QMenu * menu = new QMenu( _parent );

menu->addAction( new Action( enum value, QIcon(), tr( "Open" ), menu ) );
}

aamer4yu
15th February 2010, 09:11
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 -


QAction *action = new QAction("Open");
connect(action,trigerred(bool), someClass, mySlot(bool) );
// Add action to any menu u want... when that action is triggered you will reach mySlot function.
// Before I gave hint what to do in mySlot.

Hope you can now join the pieces together :)

nish
15th February 2010, 12:29
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.