1.创建菜单栏和菜单
1 QMenuBar *menuBar = new QMenuBar(this); 2 QMenu *file = menuBar->addMenu("&file");//用&来指定快捷键 3 file->addAction("新建");
2.创建可选择的菜单
1 QAction *newFile = file->addAction("打开(&O)"); 2 newFile->setCheckable(true);
3.创建带图标的菜单
1 file->addAction(QIcon(":/file/image/new.png"), "关闭");
4.信号槽
1 QAction *print = file->addAction("打印"); 2 connect(print, SIGNAL(triggered()), this, SLOT(onPrintTriggered()));
5.创建一组菜单,在这组菜单中只能有一个被选中
1 QActionGroup *group = new QActionGroup(this); 2 QAction *actionTxt = group->addAction("txt文件"); 3 actionTxt->setCheckable(true); 4 QAction *actionPdf = group->addAction("pdf文件"); 5 actionPdf->setCheckable(true); 6 QAction *actionExcel = group->addAction("excel文件"); 7 actionExcel->setCheckable(true); 8 actionExcel->setChecked(true); 9 file->addSeparator();//分隔栏 10 file->addAction(actionTxt); 11 file->addAction(actionPdf); 12 file->addAction(actionExcel);
6.用new来创建菜单
1 QMenu *edit = new QMenu("编辑(&E)", this); 2 menuBar->addMenu(edit); 3 QAction *selectAll = new QAction("全选", this); 4 edit->addAction(selectAll);
7.在按钮上设置菜单
1 QPushButton *button = new QPushButton(this); 2 button->setText("菜单"); 3 button->move(50, 50); 4 button->setMenu(file);
时间: 2024-10-03 15:01:28