0%

qt新建一个动作并与菜单连接

定义

1
2
3
4
5
#include <QIcon>
#include <QAction>
#include <QKeySequence>
QAction *quitAction;
QAction *aboutQtAction;

初始化

1
2
3
4
5
6
7
8
9
10
quitAction = new QAction(tr("&Quit"),this);
quitAction->setIcon(QIcon(":/quit.ico"));
quitAction->setShortcut(QKeySequence::Quit);
/// quitAction->setShortcut(tr("Ctrl+Q")); //Another short cut binding method
quitAction->setStatusTip(tr("Quit the program"));
connect(quitAction,SIGNAL(triggered()),this,SLOT(close()));

aboutQtAction = new QAction(tr("About Qt"),this);
aboutQtAction->setStatusTip(tr("Show the Qt library's About Box"));
connect(aboutQtAction,SIGNAL(triggered()),qApp,SLOT(aboutQt()));

连接

1
2
3
4
/// Link the action to the menu bar
ui->menu_File->addAction(quitAction);
ui->menu_File->addAction(aboutQtAction);
ui->menuAbout->addAction(aboutQtAction);
处无为之事,行不言之教;作而弗始,生而弗有,为而弗恃,功成不居!

欢迎关注我的其它发布渠道