当前位置: 首页 > news >正文

Qt 弹出菜单右键菜单 QMenu 设置不同颜色的子项

概述

在Qt中,可以使用样式表(StyleSheet)来自定义 QMenu 的外观,包括其子项(如菜单项QAction)的颜色。但是,这通常可以设置 QMenu 的整体样式,而不能单独设置某个子项的颜色。不过,我们可以通过自定义的 QWidgetAction实现这一目标:

效果图

实现过程

  1. 创建一个继承自 QWidgetAction 的子类 ColorAction。
  2. 添加一个的 QWidget,在 QWidget 中添加一个 QLabel。
  3. 为这个 QLabel 设置样式表。
  4. 然后,将这个自定义的 QWidgetAction 添加到 QMenu 中。

关键代码

ColorAction 类

#pragma once#include <QWidgetAction>
#include <QWidget>
#include <QLabel>  
#include <QVBoxLayout>  
#include <QStyleOption>class ColorAction : public QWidgetAction
{Q_OBJECTpublic:ColorAction(const QString text, const int nMenuHeight, QObject *parent = nullptr);~ColorAction();private:QLabel *label;QWidget *widget;
};
#include "ColorAction.h"ColorAction::ColorAction(const QString text, const int nMenuHeight, QObject *parent /*= nullptr*/) : QWidgetAction(parent)
{// 创建一个QWidget作为容器,并设置布局  widget = new QWidget();QVBoxLayout *layout = new QVBoxLayout(widget);layout->setContentsMargins(0, 0, 0, 0);// 创建QLabel并设置文字颜色  label = new QLabel(text, widget);label->setFixedHeight(nMenuHeight);label->setFont(QFont(QString::fromLocal8Bit("微软雅黑"), 10.5));label->setStyleSheet("QLabel{ padding-left:16px; color: #ff5050;background-color: rgb(255, 255, 255);} QLabel:hover{ background-color: #e9f2ff;}");layout->addWidget(label);setDefaultWidget(widget);
}ColorAction::~ColorAction()
{
}

使用 QMenu 类

#include "QMenuColorDemo.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QMouseEvent>class QPoint;QMenuColorDemo::QMenuColorDemo(QWidget *parent): QWidget(parent)
{ui.setupUi(this);	this->setFixedSize(310,200);_menu = new QMenu;_menu->setFixedWidth(115);_menu->setFont(QFont(QString::fromLocal8Bit("微软雅黑"), 10.5));int nMenuHeight = 30;_menu->setStyleSheet(QString::fromLocal8Bit("QMenu{\background:white;\border:1px solid lightgray;\padding-top:10px;\padding-bottom:10px;\}\QMenu::item{\padding:0px 20px;\height:%1px;\}\QMenu::item:selected:enabled{\background:#e9f2ff;\color:black;\}\QMenu::item:selected:!enabled{\background:transparent;\}\QMenu::separator{\height:1px;\background:#e4e4e4;\margin:5px 0px 5px 0px;\}").arg(nMenuHeight));_viewAction = new QAction(QString::fromLocal8Bit("查看"), this);_refreshAction = new QAction(QString::fromLocal8Bit("刷新"), this);_delAction = new ColorAction(QString::fromLocal8Bit("删除"), nMenuHeight, this);_menu->addAction(_viewAction);_menu->addAction(_refreshAction);_menu->addSeparator();_menu->addAction(_delAction);_btn = new QPushButton(QString::fromLocal8Bit("彩色菜单"));connect(_btn,&QPushButton::clicked,this, [=](){QPoint p = cursor().pos();_menu->exec(p);});QHBoxLayout * hLayout = new QHBoxLayout;hLayout->addWidget(_btn);hLayout->addStretch();QVBoxLayout *vLayout = new QVBoxLayout;vLayout->addItem(hLayout);vLayout->addStretch();this->setLayout(vLayout);	
}void QMenuColorDemo::mousePressEvent(QMouseEvent *event)
{if (event->button() == Qt::RightButton ){QPoint p = cursor().pos();_menu->exec(p);}
}

源码下载

https://download.csdn.net/download/qiangzi4646/89754043

http://www.lryc.cn/news/438486.html

相关文章:

  • Git换行符自动转换参数core.autocrlf的用法
  • C语言的结构体类型
  • illustrator 收集字体插件VBscript
  • 【LLM多模态】文生视频评测基准VBench
  • 通过覆写 url_for 将 flask 应用部署到子目录下
  • 攻防世界---->埃尔隆德32
  • redis短信登录模型
  • 【React】React18.2.0核心源码解读
  • 深度学习-目标检测(四)-Faster R-CNN
  • MATLAB中的无线通信系统设计有哪些最佳实践
  • Java的发展史与前景
  • 2024年上海小学生古诗文大会倒计时30多天:做几道今年的官方模拟题
  • IDEA 常用配置和开发插件
  • 还在为企微联系人烦恼?一招解决!企业微信2024年效率升级全攻略
  • 【docker npm】npm 私库
  • 完整gpt应用(自用)
  • 【信息论基础第二讲】离散信源的数学模型及其信息测度包括信源的分类、信源的数学模型、离散信源的信息测度、二元信源的条件熵联合熵
  • 在 Spring Boot 项目中连接 IBM AS/400 数据库——详细案例教程
  • VUE + NODE 历史版本安装
  • git reset 几点疑问
  • Rust Windows下编译 静态链接VCRuntime140.dll
  • 从“天宫课堂”到人工智能:中国少儿编程的未来在哪里?
  • ARM base instruction -- blr
  • 宠物猫领养馆会员管理系统---附源码72579
  • 驾驶员注意力分神状态检测系统源码分享
  • 基于less和scss 循环生成css
  • opencv之Canny边缘检测
  • springBoot 集成https
  • 数据库连接池与Druid【后端 16】
  • C#使用Access数据库使用总结