How to gray out a menu item in Qt - c++

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?

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.

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);

Related

Setting a window menu with set_menubar(), is there an 'activated' signal I can connect to run custom code when a menu is opened from the bar?

Gtk 3.0 application (with C++ and GtkMM). I use set_menubar() to set the menu bar for the main window (loaded from a resource file, i.e. a "*.ui" XML file).
At run time, I want to be able to enable/disable (gray out) menu items in accordance with availability. I know that I can use "add_action()" and "remove_action()" to associate/dissociate the actions, which will have the desired effect. However it is complicated and expensive to add/remove the actions dynamically considering that the only time it matters which actions are available is when the user has opened the menu and is looking at it.
So I was thinking that I should be able to connect to an "activate" signal of the menu bar items to adjust the appearance of their menu each time the user clicks on an item, just before the corresponding menu is displayed to the user. Any idea how I can get to the signal from the Gio::Menu resource? Is this possible in theory but an unfortunate omission in the Glib API? Or is there something I don't understand?

How do I add a Checkbox to a tool bar in MFC with a custom bitmap?

I have a C++ MFC MDI application. I have a tool bar with some buttons on it. I need to add some check boxes to this toolbar and i need them to have custom bitmaps just as my buttons do. Thanks
EDIT:
By bitmpas i refer to the pixel images that can be created using the tool bar editor in visual stuidos 2008. I would like a picture (of my creation) instead of the usual tick box.
You don't use checkboxes on toolbars.
You should rather use regular buttons in Check mode. That means that the button stays pressed when user releases it. Clicking it a second time releases the button. This is the same behaviour as a checkbox.
You can either set a toolbar button as checkable by code:
m_ToolBar.SetButtonStyle(nButtonId, TBBS_CHECKBOX);
Or by enabling the corresponding property in the resource editor.
If you want to modify the image displayed when the button is pressed, in your ON_UPDATE_COMMAND_UI handler, use m_ToolBar.GetButtonInfo() to check if the image matches the state. If not, change it using m_ToolBar.SetButtonInfo() and specify the index of an extra image added to the image list of the toolbar.
The following is a link which might help you
http://www.ucancode.net/Visual_C_Control/Place-Combo-Edit-Box-Progress-Control-On-ToolBar-CToolBar-VC-Example.htm

Add a menu resource to a dialog box

I was wondering if it is possible to add a menu resource to a dialog box. It would be easier to "design" my applications layout in multiple dialog boxes rather than the standard window, however, I will need to be able to add a menu. I would preferably be able to do this through a resource defined menu, however, I have no objections to doing it programmatically.
Right now my application is just starting like this:
DialogBox(hInst,MAKEINTRESOURCE(IDD_DIALOG_UPDATE),NULL,(DLGPROC)updateTitle);
in the main function.
Thanks for your help!
Dennis M.
Call SetMenu(hDlg, hMenu) during WM_INITDIALOG.
As Krishty commented on the accepted answer, it is easier and safer to just specify the menu ID in the designer. In the dialog resource properties, scroll down and there is a Menu option. Choose your already created menu from there. You won't see it in the designer, but when you run your app it will be there.

How do I change properties of buttons within button boxes in Qt Designer?

I have been searching online to no avail. Does anyone know how to access a button in a button box (created using the "Dialog with Buttons Right" template)?
In Designer, select the OK or Cancel button. Then open the property editor and scroll down to the QDialogButtonBox section. You can then expand the standardButtons item to see the various buttons that are available. Other properties, such as the centerButtons property, are also available.
However, designer gives you very little control over the button box.
In code, you can do many other things, such as change the text that appears on the "standard buttons." From the documentation:
findButton = new QPushButton(tr("&Find"));
findButton->setDefault(true);
moreButton = new QPushButton(tr("&More"));
moreButton->setCheckable(true);
moreButton->setAutoDefault(false);
buttonBox = new QDialogButtonBox(Qt::Vertical);
buttonBox->addButton(findButton, QDialogButtonBox::ActionRole);
buttonBox->addButton(moreButton, QDialogButtonBox::ActionRole);
As long as you give the button box a name in designer, you can set these properties in code.
I am writing this answer for the Python community. I am using PySide and faced a similar problem. I have a QDialogButtonBox and I would like to have my own buttons instead of the default ones.
I am using PySide which is more or less the exact replica of the c++ code, so I believe other c++ developers can also get something from it.
Here how I would do that:
my_ok_button = QtGui.QPushButton("My Ok Button")
my_cancel_button = QtGui.QPushButton("My Cancel Button")
ok_cancel_button = QtGui.QDialogButtonBox(QtCore.Qt.Horizontal)
ok_cancel_button.addButton(my_ok_button, QtGui.QDialogButtonBox.ButtonRole.AcceptRole)
ok_cancel_button.addButton(my_cancel_button, QtGui.QDialogButtonBox.ButtonRole.RejectRole)
I would then insert my button box to my layout like ususal:
layout.addWidget(ok_cancel_button, 1, 1)
Now later in my code I can do anything with my button. Lets change its name:
my_ok_button.setText("Some Other Name")
So then things to note here is that:
you must set the role of the buttons in the addButton() method if you
want to use functionalities given by standard buttons. E.g. if you
wish to do something like below, you need to have the button roles
set.
ok_cancel_button.accepted.connect(self.ok_method_handler)
ok_cancel_button.rejected.connect(self.close)
More information can be found here.

Creating popup menu in Qt for QTableView

I have a QTableView in the main UI of my program. I'd like to show popup menu when user right clicks on the cells of the table and take appropriate action when an option is selected from the menu.
I am using Qt Creator 1 (Qt version 4.5). How can I do that?
Check out the customContextMenuRequested signal to get the event, and use a QMenu for the menu itself. Use QTableView::indexAt to find out what, if any, cell was clicked based on the coordinates given to the signal and take the appropriate action when a menu item is clicked.