Dynamic QT4 Menu in C++ - c++

I am using C++ and QT4 on Linux. I wish to add items to the menubar at runtime. My main GUI containing the menubar was designed with QT Designer and setup in the first line of code below.
In my main window constructor I have the following test code and it compiles fine. However, the new submenu bar and its item do not appear when the program is executed. I'm guessing Qt needs to be told to update the menubar somehow. Any ideas?
// Setup the user interface
m_ui.setupUi(this);
QMenu * iObjectsMenu = new QMenu(tr("Objects"), m_ui.menubar); //m_ui.menubar.menu_View->addMenu(tr("Objectz"));
QAction * menu_testAction = new QAction(tr("Test"), m_ui.menubar);
iObjectsMenu->addAction(menu_testAction);

The way you're doing it, you need to put the menu somewhere and you haven't done so.
I do it a little differently. When you make menus in the designer they have pointers in the ui member. You can then manipulate that menu quite easily.
For example, I wanted a menu that contains all the toolbars and dock windows that can be shown/hidden. I looked at the code that makes QMainWindow::createPopup() and made my own external function construct_view_menu(QMainWindow * parent, QMenu * view_menu). It's called during the main window initialization with construct_view_menu(this, ui.menu_View). Function just appends more menus and actions to that menu. The menu_View is just an empty menu I created with the designer.
I found this the easiest way to go about it, but you can also create new menus and then append or insert them into the menubar; it's this latter step you've neglected.

I would just generate the menu in your setup code rather than the ui, keep a member variable that points to the QMenu (allocated with new)

Related

C++ Qt Action in menu not triggered by keyboard shortcut when menu open

I am building a Qt application on Linux. I have a menu bar in the main window with two menus in it, each with several actions, all of which have keyboard shortcuts associated with them. The keyboard shortcuts work when the menus are not open, but when one of the menus is open, none of them work.
The shortcuts were added to the actions with setShortcut prior to the actions being added to their respective menus with [menuobject]->addAction. All the actions have the main window as their parent. After reading QAction shortcut doesnt always work I added calls to addAction, adding the action to the main window. This did not correct the problem.
Example of the code for one of the menu items:
//In the main window constructor
gameQuit = new QAction(QString(tr("&Quit\tCtrl+Q")), this);
gameQuit->setShortcut(QKeySequence(Qt::Key_Q | Qt::CTRL));
addAction(gameQuit);
connect(gameQuit, SIGNAL(triggered()), this, SLOT(close()));
gameMenu = menuBar()->addMenu(QString(tr("&Game")));
gameMenu->addAction(gameQuit);
In QtCreator, which I assume was written with Qt, the keyboard shortcuts for the menu items do work when the menus are open, so I think there must be a way.
Thanks for any help.
Taking some advice from the comments of the cited post (which had been rebuked, which is why I didn't try it initially), I modified the shortcut context using [actionobject]->setShortcutContext(). Apparently the default does not work in my scenario.
I first tried setting to Qt::WindowShortcut, which didn't work. Qt::ApplicationShortcut did work, however, this may have shortcomings as noted in the comments of the cited post. They don't happen to matter for this particular application of mine though, so I am going to post and accept this as the answer.
Example of the correcting code:
//In the constructor of the main window, after creation of the action and
//setting of the shortcut
gameQuit->setShortcutContext(Qt::ApplicationShortcut);

Open QDialog from Main Window in Qt Designer

All I would like to do is to be able to open a new dialog from my MainWindow. I want to be able to design the dialog in Qt designer and then use the signals and slots editor to link a button press in my main window to the display of a new dialog. The dialog needs to have a couple of line edits and buttons in it and I want to avoid writing a new class in C++ every time i want a different dialog.
How can I link my main window to another dialog i created in qt designer?
You won't be able to connect the signal to startup the dialog within the designer, this will have to be in the code. But you won't need a new custom class everytime, you could easily use one class implementing different widgets.
You will have to write some c++/design some dialog everytime, since you do want to have another dialog (or at least another setup in the same dialog). You could setup the dialog to have a QStackedWidget and have an index in the constructor for having one dialog with multiple pages.

Custom menu actions in Qt Designer

I'm currently trying to use Qt Designer to build a GUI and I would like to customize slots of my menu actions.
E-g: I'd like the user to press a menu action and it'd show a widget if it is hidden or hide it if it is already visible.
Basically, what I want to do is execute some code of mine and not the default actions such as show() or hide().
So I'm wondering if I should create a subclass of QMenuBar, add custom slots to it, then create a plugin to use it inside Qt Designer or if I should create a subclass for QMenu or QAction ? Or maybe it isn't the right way to do that ?
I'm working under Visual Studio and I'm only using Qt Designer, not Qt creator.
I'm new to GUI and Qt programming and I'm a bit lost here.
Thanks in advance :)
You have basically 2 options:
Implement the custom logic in the Mainwindow sublcass.
For this, you simply add the slots required for your handling in the class, and make them available in Qt Designer. You can do this:
either in the Signal/Slot editor and click "Modify" and then click on the + Symbol. By this you make new slots available in QtDesigner;
or when your slot is called on_(senderName)_(signalName), Qt autowiring will automatically connect the signals, and you don't have to do this in code or desinger.
Create a QMenuBar subclass and implement the custom logic there.
Your case tell Qt Designer to select your specific subclass as replacement for the default QMenuBar by right-clicking on it, and select "Promote to...". In the new dialog, you can specify your custom class that will be used as replacement in actual code, but in design time a QMenuBar is used. With this mehtod, you don't have to write a separate plugin to make your class available in Qt Designer.
Note that with the second option, your custom logic will only be called when the actions are triggered through the menu bar, not by shortcuts or tool buttons
Create a slot in your class:
onMenuActionTriggered()
Use the connect() to react on action's signal:
connect(ui.myAction, SIGNAL(triggered()), this, SLOT(onMenuActionTriggered()));
In your slot you can do whatever you want.
Another solution (not my favourite one, but possible) is to use the auto-connect functionality, which means, by declaring a slot 'on_myAction_triggered()' (where myAction is the name of your QAction) you don't need to use the connect() since it is automatically connected by Qt
The menu bar is automatically added to any new form derived from QMainWindow (the default when creating a gui application, but you can create new main windows using file->new file or project... and selecting Qt->Qt Designer Form Class ).
To add options to it you simply click in the area labeled "Type here" and write your option text. When you do so an action will appear in a list in the lower part of Qt Designer. Right click on that action and select "go to slot". It will pop up a dialog with "triggered()" already selected for you. Simply click "Ok" and Qt Creator will take care of all the details and transport you to the body of the slot function.

Creating a tree-view with buttons? in QT

I am trying to make a dialog box like below in QT, the only problem is I have no idea what the widget is called. The bar on the left is like a tree-view widget, but when you click on it, it updates the text on the right. Does anybody happen to know what the widget is called or what widget(s) are required to perform this? I am using QT C++ on Windows.
There is an example with Qt showing you how to do this.
https://doc-snapshots.qt.io/4.8/dialogs-configdialog.html
If you're using Qt Creator as IDE, you can find it under the "Demos and Examples" tab in the Welcome Screen too.
It uses a QListWidget for the selector, and QStackedWidget to control the different pages. Connect the currentItemChanged signal of the list widget to change what page should be shown. Everything you'll need is in configdialog.cpp.
If you realy need to add QPushButton into QListWidget, use setItemWidget, or into ListView use QAbstractItemView::setIndexWidget

Callback for button in Qt Designer?

I just started using QtCreator tonight, and it seems it puts all of the interface stuff inside of the ui file. I followed a tutorial to create a resource for my icons, then I added them to a menu bar at the top.
I need to make a connection when one of them is clicked though, and cannot figure out how to make a callback for it.
Am I going to have to completely create them through code or is there some way to add a callback for them (rather than just making them interact with other objects).
Menu bar items are action objects. To do something when they are clicked, you need to catch the triggered() signal from the action. Read more about signals and slots here.
To do this, you need to declare a new slot in your MainWindow class. Qt also supports doing this automatically, without the need to connect anything, but I prefer doing it myself. If you're not interested, just skip this part.
First, we declare a new slot in your window class:
private slots:
void clickMenuButton();
Then, in your constructor, you need to connect the triggered signal to your new slot:
connect(ui.actionObject, SIGNAL(triggered()), this, SLOT(clickMenuButton()));
The first argument is the object that holds the signal we'll listen to (your menu button). The second is the name of the signal. The third is the object that holds the receiving slot (in this case, our window). The fourth is the slot.
And just like that, clickMenuButton() will be called whenever the action is clicked.
As I said before, Qt can also automatically connect signals to slots. The disadvantage here seems to be that you can't change the slot's name, but you don't need to connect it either.
Qt Creator supports creation of slots for widgets: in the case of your menu action, you should go to the form designer, and you should see a list of actions in your form (if you don't, find the Action Editor). Right click the action you want, and push Go to slot.... There, double click triggered().
Qt Creator will then open the new slot in your code editor, and you can do whatever you want to here!
To do that you'll need to add a QAction, add it to the menu, associate an icon with it and then create a callback for it. I'm using the VS Integration so I don't know the details of how to do it in Creator but it should be possible without creating stuff in code.
There should be somewhere an actions editor. from there you add an action, then right-click it or something to add an icon to it, then drag it do the menu and then possibly double click it to create a slot for it. This is how it works in the VS Integration atleast.