【三十一】【QT开发应用】QPushButton与QMenu
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMenu>
#include <QAction>
#include <QFileDialog>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
QMenu* pMenu=new QMenu();
QMenu* fileMenuItems=new QMenu();
QMenu* setMenuItems=new QMenu();
QList<QAction*>acList;
QList<QAction*>setList;
QAction* openFileAc=new QAction(QIcon(":/resources/file.png"),"打开文件",this);
QAction* openFloderAc=new QAction("打开文件夹",this);
QAction* openUrAc=new QAction("打开url",this);
QAction* play=new QAction(QIcon(":/resources/play.png"),"播放",this);
QAction* tools=new QAction(QIcon(":/resources/tools.png"),"工具",this);
QAction* sysSetAc=new QAction("系统设置",this);
QAction* playSetAc=new QAction("播放设置",this);
QAction* zimuSetAc=new QAction("字幕设置",this);
QAction* exitAc=new QAction(QIcon(":/resources/exit.png"),"退出",this);
//QMenu
fileMenuItems->setIcon(QIcon(":/resources/file.png"));
fileMenuItems->setTitle("文件");
setMenuItems->setTitle("设置");
setMenuItems->setIcon(QIcon(":/resources/set.png"));
//QAction
openFileAc->setShortcut(QKeySequence("Ctrl+8"));
//List
acList<<openFileAc<<openFloderAc<<openUrAc;
setList<<sysSetAc<<playSetAc<<zimuSetAc;
//add
fileMenuItems->addActions(acList);
setMenuItems->addActions(setList);
pMenu->addMenu(fileMenuItems);
pMenu->addAction(play);
pMenu->addAction(tools);
pMenu->addSeparator();
pMenu->addMenu(setMenuItems);
pMenu->addSeparator();
pMenu->addAction(exitAc);
ui->pushButton->setMenu(pMenu);
//connect
connect(openFileAc,&QAction::triggered,[=]{
QString filename=QFileDialog::getOpenFileName(this,"请选择视频文件","D:/","视频(*.mp4 *.flv)");
if(filename.isEmpty()){
return;
}
});
connect(exitAc,&QAction::triggered,[=]{
close();
});
}
MainWindow::~MainWindow()
{
delete ui;
}