QMenu 设置圆角(border radius)问题

1. 问题描述

当给QMenu设置样式时,希望使用 border-radius 为 QMenu 设置10px的圆角, 样式表如下:

QMenu{
	background-color:rgb(255,255,255);
    border-width:1px;
    border-color :rgb(0,0,0);
    border-radius:10px;
    border-style:solid
}

此时可以看到QMenu内部的填充依然是矩形,见红圈标注部分.

在这里插入图片描述

解决办法

  1. 方法1
    QMenu是一个矩形的顶层控件,因此需要在实例化QMenu后,设置QMenu实例的窗口属性。
    PySide2/PyQt5代码如下:
	menu = QMenu()
	# 设置无边框
	menu.setWindowFlags(QtCore.Qt.FramelessWindowHint)
	# 设置半透明背景
	menu.setAttribute(QtCore.Qt.WA_TranslucentBackground)
  1. 方法2
    在Pixmap上绘制一个圆角矩形作为QMenu的Mask
    C++ Qt的代码如下

    QPixmap px(this->size()); //Create pixmap with the same size of current widget
    px.fill(Qt::transparent); //Fill transparent
    QPainter p(&px);
    QBrush brush;
    brush.setStyle(Qt::SolidPattern); //For fill
    p.setBrush(brush);
    p.drawRoundedRect(this->rect(), 15.0, 15.0); //Draw filled rounded rectangle on pixmap
    this->setMask(px.mask()); //The the mask for current widget.

stack overflow上的相关问题

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值