Qt5學習:添加動做

mainwindow.h以下:this

#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以下:spa

#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"));
}
相關文章
相關標籤/搜索