Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change text in a QAction that's inside of a QMenu

Tags:

c++

qt

I have a QMenu objects to which I've added several QAction objects with a certain text. All works well, except that I would like to change the text of those QActions at runtime. I've looked at the QMenu API and didn't find any way to to get them. I also tried actions.at(x) and a few other things. What's the right way to do this?

like image 874
Gustavo Litovsky Avatar asked Oct 24 '25 14:10

Gustavo Litovsky


1 Answers

It really depends on how you are structuring your app. In some circumstances you might be saving your QAction's as members, to which you could access directly. Or you might just be saving the QMenu and populating it with QAction's. Either way, once you have your action, you just call setText(QString) on it:

// init
menu = new QMenu(this);
...
menu->addAction("foo");

// later on
QAction *action = menu->actions().at(0); // access just the first QAction
action->setText("bar");

QMenu also allows you to look up actions by a QPoint location. Again, it really depends on exactly how you will be determining which action you want to change.

like image 89
jdi Avatar answered Oct 27 '25 03:10

jdi