如果直接写代码,就用QToolBar类的成员函数AddAction,注意这是个有多个重载版本的函数,功能大同小异,自己看看帮助一目了然
Qt 5.6样例:
MainWindow.h:
#ifndef MAINWINDOW_H#define MAINWINDOW_H
#include <QObject>
#include <QMainWindow>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0)
~MainWindow()
private:
void initialAction()
void initialMenuBar()
void initialToolBar()
void initialStatusBar()
void open()
QAction *openAction
}
#endif // MAINWINDOW_HMainWindow.cpp:#include <QAction>
#include <QMenuBar>
#include <QMessageBox>
#include <QStatusBar>
#include <QToolBar>
#include "MainWindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
setWindowTitle(tr("Main Window"))
initialAction()
initialMenuBar()
initialToolBar()
initialStatusBar()
}
MainWindow::~MainWindow()
{
}
void MainWindow::initialAction()
{
openAction = new QAction(QIcon(":/images/doc-open"), QObject::tr("&Open..."), this)
openAction->setShortcuts(QKeySequence::Open)
connect(openAction, &QAction::triggered, this, &MainWindow::open)
}
void MainWindow::initialMenuBar()
{
QMenu *file = menuBar()->addMenu(QObject::tr("&File"))
file->addAction(openAction)
}
void MainWindow::initialToolBar()
{
QToolBar *toolBar = addToolBar(QObject::tr("&File"))
toolBar->addAction(openAction)
}
void MainWindow::initialStatusBar()
{
statusBar()
openAction->setStatusTip(QObject::tr("Open an existing file"))
}
void MainWindow::open()
{
QMessageBox::information(this, tr("Information"), tr("Open"))
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)