Is there a way to define a QToolbar with buttons and popup menus using Qt Designer?
Asked Answered
M

4

26

I am wanting to have a toolbar in Qt that contains either QActions or QToolbarButtons that have popup menus attached to them. I have been able to do this perfectly in the code by creating a QMenu, adding it to a QToolbarButton and then adding that to the QToolbar. My issue is that this should be able to be done completely in designer.

This is what I have done via code, I want to define the buttons and menus in qt designer:
https://static.mcmap.net/file/mcmap/ZG-Ab5ovKRltXx3eLi2paV_nX7MoWVMrKnyx/img402/7669/exmaple.png

What we are wanting to do with qt designer is to separate the code from the interface. For example this means that one person can design the form's look and components and then a programmer can take this and code the functionality behind it. We cannot accomplish this very effectively if the toolbars and menus must be designed by the programmer.

It seems like this would be a fairly common requirement for many applications, and I can't see how Qt could have forced this to be done in code instead of designer.

If anyone has any ideas as to how this is done, maybe I'm missing something in Qt?

Mosqueda answered 18/7, 2011 at 23:47 Comment(0)
M
48

Toolbar support in Qt Designer seems to be a little unintuitive and limited. I don't know of any way to add popup menus with dropdown actions directly to a toolbar in Qt Designer.

You can add pushbuttons as follows. First, right-click on your main window and select "Add Toolbar", if you don't have one already. This should add a VERY SLIM toolbar at the top of your main window (slim because it's empty).

Next, add an action in the Action Editor. It's one of the panes of Qt Designer, select View->Action Editor if you don't see it. You can create new actions here.

Next, drag actions to the toolbar to populate it with pushbuttons that trigger the actions.

I know you wanted a popup menu but as far as I know, that's the extent of what you can do with Qt Designer. It seems several others have come to the same conclusion.

Mcgray answered 26/5, 2012 at 22:23 Comment(4)
I'm surprised that this excellent answer hasn't be set as the 'answer'.Safranine
Update: As of Qt Designer 4.8.5 there still doesn't seem a way to include context menus for buttons like QToolButton. At least nothing in Qt Designer or in the Qt Designer documentation suggests that.Centrepiece
Great answer. I have never realized actions can be drag to toolbar.Thorny
This --> "First, right-click on your main window and select "Add Toolbar""Blinking
D
4

There's an awkward trick for this. You can place the desired widgets on a special QWidget (or, say QFrame) in Qt Designer and add that widget to toolbar in code.

Add to project a new Qt class named, for example QMyToolbarItem, derived from QWidget (or whatever suits). Then open QMyToolbarItem.ui in Qt designer. Add whatever complex widgets to it. You can use QComboBox or popup push buttons for the popup menu. (I already mentioned that this is awkward). Save it. In code, you just need to add the custom widget to QMainWindow's toolbar:

   ui.mainToolBar->addWidget(new QMyToolbarItem(this));

Don't forget to set size policies of QMyToolbarItem properly (for example set the minimumSize of QMyToolbarItem to a desired value). Otherwise nothing is displayed on toolbar.

Of course this is OK only when you want not normal items be added to toolbar. Note that by doing so, you lose the great power of the QAction model.

Anyway, it reasonably enough separates the UI designing stuff from code, and gives good power over complex widget design on toolbar.

At last, don't forget to consider appropriate Model View patterns to efficiently separate UI logic from the underlying business logic, if you didn't already.

Demography answered 11/2, 2013 at 14:21 Comment(0)
R
0

2021 v6.2 ran into the same thing on a mac learning this framework and qtDesigner. Added the menu bar but didn't show up on run. I tried several above ideas and they all worked, but would like to add the qtDesigner way of enabling the menu. Select the QMenuBar and scroll down, make sure nativeMenuBar is checked. This is the same thing as manually setting this property value in mainwindow.cpp with setNativeMenuBar(false); (but of course would be redundant to do it in both places)

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->menubar->setNativeMenuBar(false);
}
Rerun answered 12/12, 2021 at 19:12 Comment(0)
N
-1

On the top left in Qt Creator, when you are designing a GUI, there should be a box which includes stuff like "mainToolBar" and "menuBar". Make sure something similar to this is there. Then, at the bottom of the Qt Creator window, there is a space occupied by a Signals & Slots Editor tab and an Action Editor tab. Go to the action editor, and select the New Action option. This will let you create an action. Is this what you were looking for?

Nigrify answered 23/12, 2011 at 21:57 Comment(1)
How can you use that to create menus? I'm unable to drag actions "under" other actions on the toolbar and I don't see any other way to do it.Usually

© 2022 - 2024 — McMap. All rights reserved.