user1031431
user1031431

Reputation: 1545

How to gray out a menu item in Qt

I'm building a small program in Qt with menu bars (menuBar) using C++ and I would like to know how to gray out (eg. disable) an item of the menu when a certain variable is activated. Is it possible?

Upvotes: 14

Views: 16646

Answers (2)

user1741137
user1741137

Reputation: 5058

Looking for the index of the action is not necessarily convenient. If you have built the interface with QtCreator's form editor then you will have an action for each menu item. Their names are based on the text that you first give to the actions. For example if you interactively enter a menu item with title Foo Bar then an action named actionFoo_Bar is created for you. Just type ui->action in the code editor and watch what "name completion" QtCreator will propose.

In such a case I would consider a call like this:

ui->actionFoo_Bar.setEnabled(false);

You can even make the menu item disappear with

ui->actionFoo_Bar.setVisible(false);

Upvotes: 5

Dmitriy Kachko
Dmitriy Kachko

Reputation: 2914

If you know an index of the corresponding QAction :

QMenu::actions.at(i).setEnabled(false);

P.S. As kindly prompted below, setEnabled(bool) and setDisabled(bool) are slots (so is toggle()), so they can be connected to a signal indicating a need to change the availability of the action.

Upvotes: 18

Related Questions