Qt 自定义菜单项2

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMenu>
#include "frame.h"
#include <QWidgetAction>
#include <QLabel>
#include "mymenuaction.h"
#include <QHBoxLayout>
#include <QSlider>
#include "mywidget.h"
#include "menubtn.h"
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    menu = new QMenu(this);
    QWidgetAction *pVoice = new QWidgetAction(this);
    QWidget *pVoiceWdt = new QWidget(this);
    pVoiceWdt->setFixedSize(109,39);
    pVoiceWdt->setStyleSheet("border:0px;");
    pVoiceWdt->setStyleSheet("background-image:url(:/images/pic_xuanzhong xiao.png)");
    QLabel * label1 = new QLabel(pVoiceWdt);
    label1->move(11,11);
    label1->setText("简体中文");
    label1->setStyleSheet("width:72px;height:18px;font-size:18px;font-family:MicrosoftYaHei;font-weight:400;color:rgba(16,134,236,1);line-height:28px;");
    label1->resize(72,18);

    QLabel * label2 = new QLabel(pVoiceWdt);
    label2->resize(14,10);
    label2->move(92,16);
    label2->setStyleSheet("background-image:url(:/images/ico_xuanzhong.png)");
    pVoice->setDefaultWidget(pVoiceWdt);

    menu->addAction(pVoice);
    connect(pVoice,&QWidgetAction::triggered,[](){
        qDebug()<<"btn clicked";
    });


}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
}

void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
    //确保右键点击,然后跳出菜单.
    if (event->button() == Qt::RightButton)
    {
        menu->exec(event->globalPos());
    }
    //要继续保留QListWidget原有的点击事件.
    QMainWindow::mousePressEvent(event);

}

猜你喜欢

转载自blog.csdn.net/qq_24127015/article/details/84747229