Qt learning path (13): menu and toolbar (continued)

Source: Internet
Author: User

In the previous section, we have added qaction to the menu and toolbar. Now we need to add some images to beautify the image, and then add the signal slot so that our actions can be matched!

First, add the icon. The qaction icon is displayed in front of the menu item and on the toolbar button.

To add icons, we must first use the QT resource file. Right-click the qtcreator project and choose new file ..., Select resource file.

Click Next, select the location, and finish. For ease of use, I will create this file under the root directory. We recommend that you carefully plan the file and create it under the dedicated rsources folder. After that, A. qrc file is generated. qrc is short for Qt recource collection. It is just a common XML file and can be opened in notepad. However, we will not go into its structure here, and use qtcreator to operate this file completely,

Click the Add button, select add prefix, and then change the generated/New/prefix /. This is the prefix that needs to be provided when you use the icon in the future, starting. After the prefix is added, add an icon to the project file, select Add file, and select the icon. Save the qrc file.

The default value of the qtoolbar icon is 32*32, and the default value of the menu is 16*16. If the size of the provided icon is smaller than the required size, do not perform this operation. QT will not enlarge the image for you. Otherwise, if the provided icon file is larger than the required size, for example, 64*64, qt automatically reduces the size.

What is the picture path? We can see that the resource file view of QT uses a tree structure. The root is/, the leaf node is the image location, and the link is the path. For example, the path of this image is/open.png.

NOTE: For the sake of simplicity, we did not place the icon in a special folder. In a formal project, there should be a separate resource folder for storing resource files.

Return to mainwindow. cpp and modify the code in the constructor:

openAction = new QAction(tr("&Open"), this);openAction->setShortcut(QKeySequence::Open);openAction->setStatusTip(tr("Open a file."));openAction->setIcon(QIcon(":/Open.png")); // Add code.

We use seticon to add the icon. The added class is qicon. The constructor requires a parameter, which is a string. Because we want to use the image defined in qrc, the string starts with: followed by prefix. Because the prefix we previously defined is/, we need /, the file path is followed. This is defined in the preceding qrc. Open qrc and check the path of the image.

Now, the image is added, and click run to see the effect!

Look! We only need to modify the qaction, And the menu and toolbar have been properly processed for us, which is very convenient!

Next, add Event Response for qaction. Do you still remember that QT's event response mechanism is based on the signal slot? Clicking qaction will send a triggered () signal, so what we need to do is to name a slot and connect the signal.

mainwindow.h class MainWindow : public QMainWindow{Q_OBJECT public:MainWindow(QWidget *parent = 0);~MainWindow(); private slots:void open(); private:QAction *openAction;};

Because our open () currently only needs to be used inside the class, we can define it as private slots. Then modify the CPP file:

MainWindow::MainWindow(QWidget *parent): QMainWindow(parent){openAction = new QAction(tr("&Open"), this);openAction->setShortcut(QKeySequence::Open);openAction->setStatusTip(tr("Open a file."));openAction->setIcon(QIcon(":/Open.png"));connect(openAction, SIGNAL(triggered()), this, SLOT(open())); QMenu *file = menuBar()->addMenu(tr("&File"));file->addAction(openAction); QToolBar *toolBar = addToolBar(tr("&File"));toolBar->addAction(openAction);} void MainWindow::open(){QMessageBox::information(NULL, tr("Open"), tr("Open a file"));}

Note that a standard dialog box is displayed in the open () function without any other operations. Run the program after compilation to see the effect:

Now, the qaction has been added!

So far, qaction problems have come to an end. Finally, if you do not know how to add a sub-menu, check the qmenu API, which contains an addmenu function. That is to say, you can create a qmenu and add it!

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.