#include "widget.h"
#include "ui_widget.h"
#include "qmywidgetaction.h"
#include "qmymenu.h"
#include <QWidgetAction>
#include <QMenu>
#include <QAction>
#include <QMenuBar>
#include <QVBoxLayout>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
setupUI();
}
void Widget::setupUI()
{
ui->verticalLayout->addStretch();
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_pushButton_clicked()
{
QMenu *menu=new QMenu("菜单:常规");
QAction *action1=new QAction(QIcon(":/resource/image/向右箭头.jpg"),"action1");
QAction *action2=new QAction(QIcon(":/resource/image/向右箭头.jpg"),"action2");
QList<QAction*> list;
list.append(action1);
list.append(action2);
menu->addActions(list);
QMenuBar *menuBar=new QMenuBar;
menuBar->addMenu(menu);
menuBar->addSeparator();
ui->verticalLayout->addWidget(menuBar);
}
void Widget::on_pushButton_2_clicked()
{
QMenu *menu=new QMenu("菜单:当Widget用,添加一个布局");
QAction *action1=new QAction(QIcon(":/resource/image/向右箭头.jpg"),"action1");
QAction *action2=new QAction(QIcon(":/resource/image/向右箭头.jpg"),"action2");
QList<QAction*> list;
list.append(action1);
list.append(action2);
QVBoxLayout *vlayout=new QVBoxLayout;
vlayout->setContentsMargins(10,0,10,0);
QPushButton *button1=new QPushButton("11");
vlayout->addWidget(button1);
menu->setLayout(vlayout);
QMenuBar *menuBar=new QMenuBar;
menuBar->addMenu(menu);
menuBar->addSeparator();
ui->verticalLayout->addWidget(menuBar);
}
void Widget::on_pushButton_3_clicked()
{
QMyMenu *menu= new QMyMenu("菜单:继承QWidgetAction用法");
QAction *action1=new QAction(QIcon(":/resource/image/向右箭头.jpg"),"1"); //设置图标和内容
menu->addAction(action1);
QMyWidgetAction *myWidgetAction=new QMyWidgetAction(menu);
menu->addSeparator(); //添加分割线
menu->addAction(myWidgetAction);
QMenuBar *menuBar=new QMenuBar(this);
menuBar->setStyleSheet("QMenuBar{background-color:red}"
"QMenuBar:hover{background-color:blue}");
menuBar->addMenu(menu);
ui->verticalLayout->addWidget(menuBar);
ui->verticalLayout->addStretch();
}