您的位置:首页 > 编程语言 > Qt开发

QT5入门之11 - 工具栏

2015-10-15 17:32 453 查看
工具栏和菜单栏是联系在一起的。

QAction能够根据添加的位置来改变自己的样子——如果添加到菜单中,就会显示成一个菜单项;如果添加到工具条,就会显示成一个按钮。

openAction = new QAction(QIcon(":/images/open"), tr("&Open..."), this);
openAction->setShortcuts(QKeySequence::Open);
openAction->setStatusTip(tr("Open an existing file"));
connect(openAction, &QAction::triggered, this, &MainWindow::open);//&MainWindow?
QMenu *file = menuBar()->addMenu(tr("&File"));
file->addAction(openAction);

exitAction = new QAction(QIcon(":/images/exit"), tr("&Exit..."), this);
exitAction->setShortcuts(QKeySequence::Close);
exitAction->setStatusTip(tr("exit the app"));
connect(exitAction, &QAction::triggered, this, &MainWindow::exit);
file->addAction(exitAction);
//
QMenu *edit = menuBar()->addMenu(tr("&Edit"));
edit->addAction(exitAction);

QToolBar *toolBar = addToolBar(tr("&File"));
toolBar->addAction(openAction);
toolBar->addAction(exitAction);
//  ui->mainToolBar->addAction(openAction);
//   ui->mainToolBar->addAction(exitAction);


效果如下:



void exit();

其中 QIcon(“:/images/exit”) 是引用资源文件中的图片,需要添加qrc文件(菜单文件-新建-Qt-Qt resourcefile)。

qrc文件内容如下(右键qrc-添加现有文件):

<RCC>
<qresource prefix="/">
<file>images/open.png</file>
<file>images/exit.png</file>
</qresource>
</RCC>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: