QT Learning Pathway (12): Menus and sidebar

Source: Internet
Author: User
Tags definition constructor documentation

On the basis of the previous Qmainwindow, we began to build our application. Although there is already a framework, but, exactly, we still have a line of code did not write it! The work below is not so easy! In this section, we add menus and toolbars to our framework.

Like the action in swing, there is a similar class in Qt called Qaction. As the name suggests, the Qaction class holds information about this action, which is action, such as its text description, icon, shortcut key, callback function (that is, the signal slot), and so on. Amazingly, qaction can change its appearance depending on where it is added--if added to the menu, it will appear as a single item, and if added to the sidebar, it will appear as a button. That's why you put menus and buttons in one section. Now start learning!

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

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

Be careful, don't forget the qaction declaration of the Class! Or it will be an error!

Then we'll add the qaction definition to the CPP file. For the sake of simplicity, we define it directly 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 are using the

QAction(const QString &text, QObject* parent);

This one. It has two parameters, and the first text is a textual description of the action, used to display textual information, such as text in a menu, and the second is parent, and generally, we usually pass the this pointer on. We do not need to care what this parent parameter is specifically about, it is to indicate the parent component of this qaction, when the parent component is destroyed, such as delete or automatically destroyed by the system, the associated qaction is automatically destroyed.

If you still don't understand what the constructor parameters mean, or want to learn more about the Qaction class, you'll need to flip through its API documentation. As I said before about the use of the API, here is no longer to repeat. This is also a way to learn Qt, because Qt is a very large library, we can not be exhaustive, so only for the use of things, as you want to achieve the function, you need to check the document yourself.

In the second sentence, we use the Setshortcut function. Shortcut is the shortcut key to this action. QT's qkeysequence has defined a number of built-in shortcuts, such as the open we use. You can get all the list of shortcuts by looking through the API documentation, or enter in the Qtcreator:: After that, there will be a system full of automatic complement display. What is the difference between this and our own definition? In simple terms, we can completely define a TR ("Ctrl+o") to implement shortcut keys. The reason is that this is a manifestation of QT's cross-platform nature. For example, PC keyboard and Mac keyboard is not the same, some keys on the PC keyboard, and the Max keyboard may not exist, or vice versa, so it is recommended to use the Qkeysequence class to add shortcut keys, so that it will be based on the platform to define different shortcut keys.

The third sentence is the Setstatustip function. This is the prompt statement that adds the status bar. The status bar is the bottom section of the main window. Now our program has not added a status bar, so you do not see what effect.

The next thing to do is to add this qaction to the menu and the sidebar:

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

Qmainwindow has a menubar () function that returns the menu bar, which is the top one. If it does not exist, it is automatically created, and if it already exists, it returns the pointer to that menu bar. Add a menu directly using the return value, which is AddMenu, and the parameter is a qstring, which is the name of the menu displayed. Then use this Qmenu pointer to add this qaction. Similarly, a toolbar is added using the return value of the Addtoolbar function, and the qaction is added to the top.

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.