Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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:
http://img402.imageshack.us/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?

like image 233
Eddie Loeffen Avatar asked Jul 18 '11 23:07

Eddie Loeffen


2 Answers

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.

like image 93
moodboom Avatar answered Sep 18 '22 12:09

moodboom


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.

like image 24
Masood Khaari Avatar answered Sep 21 '22 12:09

Masood Khaari