QCheckBox的qss部分

文章讲述了在Qt中使用QSS样式表来定制QCheckBox的选中与未选中状态,包括圆角边框、背景颜色和对勾图标的显示。在解决选中时的对勾晃动问题上,通过调整checked状态下的width和height来消除切换时的视觉差异。
摘要由CSDN通过智能技术生成

目标效果: 

 

 qt中如何实现这样的效果呢?

选中时:圆角边框,蓝色背景,白色对勾。checked

未选中时:圆角边框,白色背景,没有对勾。unchecked

QCheckBox::indicator对应着这个小框框

宽度和高度也需要调整。

初版:

QCheckBox::indicator:checked
{
	border-radius:5px;
	background-color: rgb(0, 161, 214);
	width: 20px;
    height: 20px;
}
QCheckBox::indicator:unchecked
{
	border-radius:5px;
	background-color: rgb(255,255,255);
    border-style: solid;
    border-width: 2px;
    border-color: rgb(229, 231, 235);	
	width: 20px;
    height: 20px;
}

关于选中时的对勾?

border-image: url(:/images/对勾_块.png);

但这样有缺点, 在切换的时候会看到一种晃动。是间距的变化。

而b站的页面却没有这种晃动。

解决方法:修改checked状态下的高和宽为24px后即可消除晃动。

思考:

 可以这么理解,width和height决定的是background的高和宽,实际显示时border的部分也会显示。

完整qss内容:

QCheckBox::indicator:checked
{
	border-radius:5px;
	background-color: rgb(0, 161, 214);
	width: 24px;
    height: 24px;
	border-image: url(:/images/对勾_块.png);
}
QCheckBox::indicator:unchecked
{
	border-radius:5px;
	background-color: rgb(255,255,255);
    border-style: solid;
    border-width: 2px;
    border-color: rgb(229, 231, 235);	
	width: 20px;
    height: 20px;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lpl还在学习的路上

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值