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);

}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值