使用setStyleSheet怎么样才不影响子组件:解决方法

本文探讨了在使用级联样式表(CSS)时如何精确地为特定组件设置样式,避免影响到其他子组件。通过添加前缀'.',可以实现对目标组件的独立样式设定,而不干扰页面上其他元素的样式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用了级联样式
setStyleSheet("border-image: url(:/image/serialport.png)");
结果子组件 都影响。

setStyleSheet(".QWidget{border-image: url(:/image/serialport.png)}");
前面加个 . 就可以单独对需要处理的出来了。

#include "microphonecard.h" MicrophoneCard::MicrophoneCard(QSharedPointer<EntityMicrophoneList> micList, int w, int h, QWidget *parent) : QWidget(parent) { this->cardList = micList; this->setObjectName("paaa"); // 设置背景颜色 QString styleSheet = QString( "background-color: %1; " "border-radius: %2px; " "border: 1px solid %3;" ) .arg(cardList->getCardBackground()) .arg(cardList->getRadius()) .arg(cardList->getCardBackground()); this->setFixedSize(w, h); this->setStyleSheet(styleSheet); m_gridLayout = new QGridLayout(this); m_gridLayout->setSizeConstraint(QLayout::SetMinAndMaxSize); // 元素间距 m_gridLayout->setSpacing(0); // 边距 m_gridLayout->setContentsMargins(0, 0, 0, 0); icon = new QLabel(this); IconHelper::setIconFontIndex(4); IconHelper::setIcon(icon,0xe60b,24); icon->setFixedSize(w / 5, h / 1); // 固定子组件尺寸 icon->setStyleSheet("background: transparent; border: none;"); m_gridLayout->addWidget(icon, 0, 0); people = new QLabel(this); people->setText("参会人员"); people->setFixedSize(w / 5, h / 1); // 固定子组件尺寸 m_gridLayout->addWidget(people, 0, 1); slider = new QSlider(Qt::Horizontal,this); // 设置范围(默认0-99) slider->setRange(0, 100); // 设置初始值 slider->setValue(0); // 设置步长 slider->setSingleStep(1); // 方向键步长 slider->setPageStep(10); // 鼠标点击滑块槽时的步长 slider->setFixedSize(w / 5, h / 1); // 固定子组件尺寸 slider->setStyleSheet(R"( QSlider::groove:horizontal { height: 8px; background: #ddd; } QSlider::handle:horizontal { width: 16px; margin: -6px 0; background: #2196F3; border-radius: 8px; } )"); m_gridLayout->addWidget(slider, 0, 2); decibel = new QLabel(this); decibel->setText("50db"); decibel->setFixedSize(w / 5, h / 1); // 固定子组件尺寸 m_gridLayout->addWidget(decibel, 0, 3); switchButton = new QLabel(this); IconHelper::setIconFontIndex(4); IconHelper::setIcon(switchButton,0xe60b,24); switchButton->setFixedSize(w / 5, h / 1); // 固定子组件尺寸 m_gridLayout->addWidget(switchButton, 0, 4); } 只有子组件演示改变,this样式没变化
最新发布
03-21
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值