qt gui programming

I seldom play with the bells and whistles of Qt gui elements. All I used to do is adding some buttons on a QDialog derived object and connecting them to handlers. That is all I knew about the GUI programming in the past. But if you like the whistling GUI elements and you want a beautiful application window, you may need to learn something about those GUI classes. You’d better learn GUI programming from a QMainWindow demo as QMainWindow integrates some important GUI components such as menu bar and status bar.


How to add a menu bar?

As I said, QMainWindow has an integrated menu bar so you do not need to create one. The function menuBar() returns the current menu bar object. All you need to do is adding menus to the menu bar object:


The menus, however, should be created by yourself:

QMenu *fileMenu = new QMenu(tr("&File"));
fileMenu->addAction(tr("&New Window"), this, &BrowserWindow::handleNewWindowTriggered, QKeySequence::New);
QAction *newTabAction = new QAction(tr("New &Tab"), this);
connect(newTabAction, &QAction::triggered, tabWidget, &TabWidget::createTab);

A menu has a menu title and several menu items. The menu title can be specified as the parameter of QMenu’s constructor. The menu items can be added to the menu as actions using the addAction function. The menu items are called actions because they are connected to some action when user clicks on them. The addAction function has several versions. The simplest version of addAction takes an ready-to-use action as its parameter. An action object gets its name(seen as the pull down menu item) from its constructor. The handler connected to the action is specified using ordinary signal-slot mechanism(connect the triggered signal of the action to a slot). An action can also be assigned to a shortcut key, i.e.,


will set the key sequence  QKeySequence::AddTab(const 19) as the shortcut key for the action. But what on earth is the key sequence QKeySequence::AddTab? Well, it depends on the system. On my system, QKeySequence::AddTab stands for Ctrl+T. You can see the key sequence right next to the menu item. Pressing Ctrl T has the same effect as clicking the menu item. But what’s the meaning of the underline in a menu item? It is another kind of shortcut key, which can be specified by adding a & before the key character in the menu item string. When the pull-down menu is displayed, you can press the key with a underline in some menu item, which has the same effect as clicking that menu item.  So every menu item that belongs to the same menu must have unique short key. This kind of short keys only work when the pull down menu is displayed,while the first kind of short keys work do not have this requirement. The other version of addAction takes the menu item string, the handler, the shortcut key as its parameter, which completes the work of constructing an action object. A menu can has seperators between menu items that serve to group menu items.

How to create a tool bar?

The tool bar looks like several icons and widgets arranged horizonally. Every tool in the toolbar is an action(like a menu item) or a widget. Use toolbar->addAction() and toolbar->addWidget to add them. Of course, you should create the tool bar first:

toolbar=new QToolBar("toolbarname");


The toolbarname, howerver, does not show on the toolbar. To visualize the tools in the toolbar, every action added to the toolbar should set an icon:


The actions in the menu usually do not need to set icons. The action in the toolbar can have a tooltip:


This way, when user hovers the mouse over the tool’s icon, he can get some useful help information about this tool. For the actions in the toolbar, we can also set their shortcut keys as we do for the menu actions. After creating the tool bar, we can add it to the QMainWindow:


 How to create a status bar?

For QMainWindow, you do not need to create a status bar as it already has an integrated status bar which is located at the bottom of the window. You just get it and show message on it:


The main part of QMainWindow is called central widget. You should create it yourself and add it to the QMainWindow as follows:

QWidget *centralWidget = new QWidget(this);

The position of central widget is determined beforehand: below the tool bar and above the status bar.






Posted in

Comments are closed, but trackbacks and pingbacks are open.