Qt5学习:添加动作

mainwindow.h如下:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();
private:
    void open();
    QAction *openAction;
};

#endif // MAINWINDOW_H

mainwindow.cpp如下:

#include "mainwindow.h"
#include <QAction>
#include <QMenuBar>
#include <QMessageBox>
#include <QStatusBar>
#include <QToolBar>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    setWindowTitle(tr("Main Window"));
    this->openAction = new QAction(QIcon(":/images/doc-open"), tr("&Open..."), this);
    this->openAction->setShortcuts(QKeySequence::Open);
    this->openAction->setStatusTip(tr("Open an existing file"));
    connect(this->openAction, &QAction::triggered, this, &MainWindow::open);

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

}

MainWindow::~MainWindow()
{

}
void MainWindow::open() {
    QMessageBox::information(this, tr("Information"), tr("Open"));
}

 

posted on 2015-10-31 11:42  SomeBod_Y  阅读(666)  评论(0编辑  收藏  举报