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

概述

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

效果图

实现过程

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

关键代码

ColorAction 类

cpp 复制代码
#pragma once

#include <QWidgetAction>
#include <QWidget>
#include <QLabel>  
#include <QVBoxLayout>  
#include <QStyleOption>

class ColorAction : public QWidgetAction
{
	Q_OBJECT

public:
	ColorAction(const QString text, const int nMenuHeight, QObject *parent = nullptr);
	~ColorAction();

private:
	QLabel *label;
	QWidget *widget;
};
cpp 复制代码
#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 类

cpp 复制代码
#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

相关推荐
别动我齐刘海1 天前
机器人运动控制学习2——基础进阶
c++·人工智能·神经网络·学习·目标检测·机器学习·机器人
小庞在加油1 天前
WinDbg实战:QT/跨平台项目死锁与无响应问题排查指南
开发语言·qt·windbg·工具
傻啦嘿哟1 天前
英雄联盟皮肤爬虫:爬取全皮肤价格与特效,做比价工具
java·c++·爬虫
码匠许师傅1 天前
【C++ 面试真题】24. 聊聊 C++ 的时间日期处理
java·c++·面试
闻道且行之1 天前
图片处理助手|泊松融合原理 + C++ 工程实现,seamlessClone 三模式一次讲透
数据库·c++·人工智能·opencv
hy.z_7771 天前
【QT】1.QT初识(背景介绍、搭建开发环境、Qt Creator、程序、项目文件解析、编程注意事项)
qt
莫浅子1 天前
day01-USB架构与枚举
c++·单片机·嵌入式驱动
小欣加油1 天前
leetcode3069 将元素分配到两个数组中I
数据结构·c++·算法·leetcode·职场和发展
不会代码的小猴1 天前
7. JSON
开发语言·c++·笔记·qt·算法·json
沫璃染墨1 天前
《Qt从零入门系列(六):信号与槽进阶——从多种连接方式到Lambda表达式》
开发语言·c++·qt·代码规范·设计规范·qt5