Qt learning path (12): menu and toolbar

Source: Internet
Author: User

Based on the previous qmainwindow, we started to build our applications. Although there is already a framework, we still haven't written a line of code! The following work is not that easy! In this section, we will add menus and toolbar for our framework.

Like the action in swing, QT also has a similar class called qaction. As the name suggests, the qaction class stores information about this action, that is, the action, such as its text description, icon, shortcut key, callback function (that is, the signal slot), and so on. The magic is that qaction can change its appearance based on the position added. If it is added to the menu, it will be displayed as a menu item. If it is added to the toolbar, A button is displayed. This is why menu and button should be placed in the Section. Start learning now!

First, I want to add an open command. Add a private qaction variable to the header file:

class QAcion;//...private:QAction *openAction;//...

Note: Do not forget the pre-declaration of the qaction class! Otherwise, an error will be reported!

Then we will add the definition of qaction to the CPP file. For simplicity, we define it in the constructor:

openAction = new QAction(tr("&Open"), this);openAction->setShortcut(QKeySequence::Open);openAction->setStatusTip(tr("Open a file."));

The first line of code creates a qaction object. Qaction has several overloaded constructors. We use

QAction(const QString &text, QObject* parent)

This one. It has two parameters. The first text is the text description of the action, used to display text information, such as text in the menu; the second is parent. Generally, we usually pass in the this pointer. We do not need to care about what the parent parameter is. Its function is to specify the parent component of the qaction. When the parent component is destroyed, such as delete or is automatically destroyed by the system, the qaction associated with it is also automatically destroyed.

If you still don't understand what the constructor parameters mean, or want to learn more about the qaction class, you need to read its API documentation. I have discussed how to use APIs. I will not describe them here. This is also a way to learn QT, because QT is a large library and we cannot cover everything. Therefore, we only want to talk about the things we use. As for the functions you want to implement, you need to check the document yourself.

In the second sentence, we use the setshortcut function. Shortcut cut is the shortcut for this action. Qt's qkeysequence has defined many built-in shortcuts for us, such as open. You can view the API documentation to obtain a list of all shortcut keys, or enter: In qtcreator to display the system's auto-completion function. What is the difference between this and what we define? Simply put, we can define a TR ("Ctrl + O") by ourselves to achieve the shortcut key. The reason is that this is the embodiment of QT cross-platform. For example, the PC and Mac keyboards are different. Some keys are available on the PC keyboard, but the max keyboard may not exist, or vice versa. Therefore, we recommend that you use the qkeysequence class to add shortcuts, in this way, different shortcut keys are defined based on different platforms.

The third sentence is the setstatustip function. This is the prompt statement for adding the status bar. The status bar is the bottom of the main window. Currently, the status bar has not been added to our program, so you cannot see any function.

Add this qaction to the menu and toolbar:

QMenu *file = menuBar()->addMenu(tr("&File"));file->addAction(openAction); QToolBar *toolBar = addToolBar(tr("&File"));toolBar->addAction(openAction);

Qmainwindow has a menubar () function, which returns the menu bar, that is, the top one. If it does not exist, it is automatically created. if it already exists, the pointer to the menu bar is returned. Directly use the return value to add a menu, that is, addmenu. The parameter is a qstring, that is, the name of the displayed menu. Then, use the qmenu pointer to add the qaction. Similarly, a toolbar is added using the return value of the addtoolbar function and the qaction is added to the toolbar.

All right, the main code has been written. However, if you only modify this, it is not compiled! As the menubar () function returns a qmenubar pointer, but you do not include its header file! Although the qmenubar class is not clearly written, you have actually used its addmenu function, so pay attention to it!

The following code is provided:

1. mainwindow.h#ifndef MAINWINDOW_H#define MAINWINDOW_H #include <QtGui/QMainWindow> class QAction; class MainWindow : public QMainWindow{Q_OBJECT public:MainWindow(QWidget *parent = 0);~MainWindow(); private:QAction *openAction;}; #endif // MAINWINDOW_H 2. mainwindow.cpp #include <QtGui/QAction>#include <QtGui/QMenu>#include <QtGui/QMenuBar>#include <QtGui/QKeySequence>#include <QtGui/QToolBar>#include "mainwindow.h" MainWindow::MainWindow(QWidget *parent): QMainWindow(parent){openAction = new QAction(tr("&Open"), this);openAction-&gt;setShortcut(QKeySequence::Open);openAction-&gt;setStatusTip(tr("Open a file.")); QMenu *file = menuBar()-&gt;addMenu(tr("&File"));file-&gt;addAction(openAction); QToolBar *toolBar = addToolBar(tr("&File"));toolBar-&gt;addAction(openAction);} MainWindow::~MainWindow(){ }

Main. cpp is not modified, so it is not provided here. The running result is as follows:

Ugly, right? However, we have added menus and toolbar! Click Alt + F on the keyboard, because this is the shortcut key we have defined for it. Although it is ugly at present, it will become beautiful in the future! Think about it. Linux KDE Desktop is implemented by QT!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.