Qt 自定義選單項2
阿新 • • 發佈:2018-12-14
#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); }