QT中对菜单处理例子: :
C:\Qt\Qt5.5.1\Examples\Qt-5.5\widgets\mainwindows\menus\menus.pro
QMenu的个性化定制
参考http://blog.sina.com.cn/s/blog_a6fb6cc90101fxsc.html
http://blog.chinaunix.net/xmlrpc.php?r=blog/index&uid=29181887&cid=184923&pattern=list
C:\Qt\Qt5.5.1\Examples\Qt-5.5\widgets\mainwindows\menus\menus.pro
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
class QAction;
class QActionGroup;
class QLabel;
class QMenu;
QT_END_NAMESPACE
//! [0]
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow();
protected:
void contextMenuEvent(QContextMenuEvent *event) Q_DECL_OVERRIDE;
//! [0]
//! [1]
private slots:
void newFile();
void open();
void save();
void print();
void undo();
void redo();
void cut();
void copy();
void paste();
void bold();
void italic();
void leftAlign();
void rightAlign();
void justify();
void center();
void setLineSpacing();
void setParagraphSpacing();
void about();
void aboutQt();
//! [1]
//! [2]
private:
void createActions();
void createMenus();
//! [2]
//! [3]
QMenu *fileMenu;
QMenu *editMenu;
QMenu *formatMenu;
QMenu *helpMenu;
QActionGroup *alignmentGroup;
QAction *newAct;
QAction *openAct;
QAction *saveAct;
QAction *printAct;
QAction *exitAct;
QAction *undoAct;
QAction *redoAct;
QAction *cutAct;
QAction *copyAct;
QAction *pasteAct;
QAction *boldAct;
QAction *italicAct;
QAction *leftAlignAct;
QAction *rightAlignAct;
QAction *justifyAct;
QAction *centerAct;
QAction *setLineSpacingAct;
QAction *setParagraphSpacingAct;
QAction *aboutAct;
QAction *aboutQtAct;
QLabel *infoLabel;
};
//! [3]
#endif
mainwindow.cpp :
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QTextStream>
#include <QDomNode>
#include <QHash>
#include <QAbstractItemModel>
#include <QDomDocument>
#include <QModelIndex>
#include <QString>
#include <QDebug>
#include <QMap>
#include <QMessageBox>
#include <QContextMenuEvent>
QT_BEGIN_NAMESPACE
class QMenu;
class QTreeView;
class QWidgetAction;
class QContextMenuEvent;
QT_END_NAMESPACE
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->createActions_0();
this->creatActions_1();
this->addActions();
connect(ui->pushButton_4,SIGNAL(clicked()),this,SLOT(pushButton_4Slot()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::createActions_0()
{
myAllMenu = new QMenu();
show_action_0 = new QAction("show_action_0",myAllMenu);
show_action_1 = new QAction("show_action_1",myAllMenu);
show_action_2 = new QAction("show_action_2",myAllMenu);
connect(show_action_0, SIGNAL(triggered()), this, SLOT(show_action_0_slot()));
connect(show_action_1, SIGNAL(triggered()), this, SLOT(show_action_1_slot()));
connect(show_action_2, SIGNAL(triggered()), this, SLOT(show_action_2_slot()));
}
void MainWindow::show_action_1_slot()
{
QMessageBox msgBox;
msgBox.setText("show_action_1_slot");
msgBox.exec();
}
void MainWindow::show_action_2_slot()
{
QMessageBox msgBox;
msgBox.setText("show_action_2_slot");
msgBox.exec();
}
void MainWindow::show_action_0_slot()
{
QMessageBox msgBox;
msgBox.setText("show_action_0_slot.");
msgBox.exec();
}
void MainWindow::pushButton_4Slot()
{
}
void MainWindow::contextMenuEvent(QContextMenuEvent *event)
{
myAllMenu->exec(event->globalPos());
//myAllMenu->exec(event->pos());
// QMenuBar * menuBar;
// menuBar->addMenu(pop_menu);
//this->setMenuBar(pop_menu);
//this->setMenuBar(menuBar);
}
void MainWindow::creatActions_1()
{
play_widget = new QWidget();
play_widget_action = new QWidgetAction(myAllMenu);
button0 = new QPushButton("button0");
button1 = new QPushButton("button1");
button2 = new QPushButton("button2");
QLineEdit *lineEdit0=new QLineEdit("lineEdit0");
QComboBox *ComboBox0=new QComboBox();
ComboBox0->addItem("ComboBox0-items0");
ComboBox0->addItem("ComboBox0-items1");
ComboBox0->addItem("ComboBox0-items2");
ComboBox0->setCurrentIndex(1);
label0 = new QLabel("label0");
label0->setText("player");
label0->setEnabled(false);
label0->setFixedWidth(20);
label0->setAlignment(Qt::AlignCenter);
button0->setObjectName("button0");
button1->setObjectName("button1");
button2->setObjectName("button2");
QHBoxLayout *hlayout = new QHBoxLayout();
hlayout->addWidget(button0);
hlayout->addWidget(button1);
hlayout->addWidget(button2);
hlayout->addWidget(lineEdit0);
hlayout->addWidget(ComboBox0);
hlayout->setSpacing(0);
hlayout->setContentsMargins(20, 0, 20, 0);
QVBoxLayout *vlayout = new QVBoxLayout();
vlayout->addLayout(hlayout);
vlayout->addWidget(label0, 0, Qt::AlignCenter);
vlayout->setSpacing(5);
vlayout->setContentsMargins(0, 0, 0, 5);
play_widget->setLayout(vlayout);
play_widget_action->setDefaultWidget(play_widget);
play_widget->show();
connect(button0, SIGNAL(clicked()), this, SLOT(button0_slot()));
connect(button1, SIGNAL(clicked()), this, SLOT(button1_slot()));
connect(button2, SIGNAL(clicked()), this, SLOT(button2_slot()));
}
void MainWindow::button0_slot()
{
QMessageBox msgBox;
msgBox.setText("button0_slot.");
msgBox.exec();
}
void MainWindow::button1_slot()
{
QMessageBox msgBox;
msgBox.setText("button1_slot.");
msgBox.exec();
}
void MainWindow::button2_slot()
{
QMessageBox msgBox;
msgBox.setText("button2_slot.");
msgBox.exec();
}
void MainWindow::addActions()
{
myAllMenu = menuBar()->addMenu(tr("&pop_menu"));
myAllMenu->addAction(play_widget_action);
myAllMenu->addSeparator();
myAllMenu->addAction(show_action_0);
myAllMenu->addSeparator();
myAllMenu->addAction(show_action_1);
myAllMenu->addAction(show_action_2);
}
main.cpp :
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}