【QSS样式表 - ⑯】:QTableView、QTableWidget控件样式

QTableView、QTableWidget控件样式

QTableWidget 是 QTableView的子类。

  1. 常用属性
    在这里插入图片描述
  2. 子控件
    在这里插入图片描述

QSS示例

  1. 创建一个新的 QMainWindow 窗口,拖入 QTableView 和 QTableWidget 控件,并在 QTableWidget 中建立一些项:
    在这里插入图片描述

  2. 如下所示。

    /*头部样式*/
    QTableView QHeaderView::section{
         
         
    	background-color: blue;
    	f
/* 整体背景色 */ QMainWindow { background-color: rgb(10, 30, 45); } /* QLabel 样式设置 */ QLabel { color: rgb(255, 255, 255); } QLabel::disabled { color: rgb(90, 110, 130); } QToolBar, QFrame, QDialog { background-color: rgb(11, 53, 89); } QToolButton { color: rgb(255, 255, 255); } QToolButton::hover { background-color: rgba(0, 91, 174, 204); border: 1px solid rgb(25, 130, 227); border-radius: 4px; } /* QTableWidget QTableView 样式设置 */ QTableWidget, QTableView { background-color: rgb(9, 41, 68); color: rgb(255, 255, 255); gridline-color: rgb(30, 65, 105); } QTableWidget::item::selected, QTableView::item::selected { background-color: rgb(2, 110, 210); } QTableWidget::item::hover, QTableView::item::hover { background-color: rgb(1, 91, 174); } QHeaderView::section { background-color: rgb(0, 60, 109) !important; border: 0px solid red !important; border-bottom: 1px solid gray !important; color: rgb(255, 255, 255) !important; gridline-color: rgb(30, 65, 105) !important; } QHeaderView::section:horizontal, QTableView QHeaderView { border-right: 1px solid gray; border-left: 1px solid gray; } QTableWidget QTableCornerButton::section, QTableView QTableCornerButton::section { background-color: rgb(0, 60, 109); } /* QPushButton 样式设置 */ QPushButton { color: rgb(255, 255, 255); background-color: rgb(0, 91, 174); border-radius: 4px; padding: 4px; } QPushButton::hover { border: 1px solid rgb(255, 255, 255); } QPushButton::pressed { background-color: rgb(25, 130, 227); border: 0px; } QPushButton::disabled { color: rgb(105, 140, 165); background-color: rgb(35, 85, 140); } /* QLineEidt 样式设置 */ QLineEdit { border: 1px solid rgb(80, 125, 170); background-color: rgba(5, 25, 53, 204); color: rgb(255, 255, 255); } QLineEdit::hover { border: 1px solid rgb(22, 148, 254); } QLineEdit::disabled { border: 1px solid rgb(34, 75, 115); } /* QComboBox 样式设置 */ QComboBox { border: 1px solid rgb(80, 125, 170); color: rgb(255, 255, 255); background-color: rgba(5, 26, 53, 204); } QComboBox::hover { border: 1px solid rgb(22, 148, 254); } QComboBox QAbstractItemView::item { color: rgb(255, 255, 255); height: 35px; } QComboBox QAbstractItemView::item::hover { border: 1px solid rgb(22, 148, 254); background-color: rgb(55, 135, 218); color: rgb(255, 255, 255); } QComboBox::disabled { border: 1px solid rgb(34, 75, 115); color: rgb(90, 110, 130); background-color: rgba(5, 26, 53, 204); } /* QSpinBox 样式设置 */ QSpinBox { border: 1px solid rgb(80, 125, 170); color: rgb(255, 255, 255); background-color: rgba(5, 26, 53, 204); } QSpinBox::hover { border: 1px solid rgb(22, 148, 254); color: rgb(255, 255, 255); } QSpinBox::up-button::hover, QSpinBox::down-button::hover { border: 1px solid rgb(22, 148, 254); } QSpinBox::up-button::pressed, QSpinBox::down-button::pressed { border: 1px solid rgb(22, 148, 254); color: rgb(255, 255, 255); background-color: rgba(15, 95, 165, 204); } QSpinBox::disabled { border: 1px solid rgb(34, 75, 115); color: rgb(90, 110, 130); background-color: rgba(5, 26, 53, 204); } /* QTabWidget 样式设置 */ QTabWidget { border: none; } QTabWidget::pane { border: none; } QTabBar::tab { background-color: rgb(0, 91, 174); color: rgb(255, 255, 255); padding: 4px; border: none; border-bottom: 2xp solid rgb(35, 85, 140); } QTabBar::tab::hover { background-color: rgb(0, 91, 174); color: rgb(255, 255, 255); border-radius: 4px; border: 1px solid rgb(255, 255, 255); border-width: 1px 1px 2px 1px; } QTabBar::tab::selected { background-color: rgb(25, 130, 227); color: rgb(255, 255, 255); border: none; border-bottom: 2px solid rgb(35, 85, 140); } QTabBar::disabled { background-color: rgb(35, 85, 140); color: rgb(105, 140, 165); border: none; border-bottom: 2px solid rgb(35, 85, 140); } /* QSilder 样式设置 */ QSlider::groove:horizontal { height: 7px; background-color: rgb(22, 61, 100); border-radius: 3px; } QSlider::sub-page:horizontal { background-color: rgb(0, 255, 252); height: 6px; border-radius: 3px; } QSlider::handle { background-color: rgb(81, 75, 255); width: 3px; margin-top: -1px; margin-bottom: -1px; margin-left: 0; margin-right: 0; border-radius: 3px; border: 0px ; border-style: solid; border-color: #ffffff; } /* QDialog 样式设置 */ QDialog { background-color: rgba(25, 55, 85, 204); border: 1px solid rgba(15, 80, 135, 204); border-radius: 2px; font-size: 17px; margin: 0px; } /* QGroupBox 控件样式 */ QGroupBox { border: 1px solid rgba(15, 80, 135, 204); margin-top: 2ex; padding: 5px; } QGroupBox::title { color: rgb(255, 255, 255); subcontrol-origin: margin; left: 10px; } /* QCheckBox 控件样式 */ QCheckBox { color: rgb(255, 255, 255); } QCheckBox::indicator { border: 1px solid rgb(255, 255, 255); height: 12px; width: 12px; } QCheckBox::indicator::hover { border: 1px solid rgb(0, 255, 252); } QCheckBox::indicator::checked { border: 1px solid rgb(0, 255, 252); image: url(:/images/images/check.svg); } QCheckBox::disabled { border: 1px solid rgb(75, 100, 130); } /* QTextEdit 样式设置 */ QTextEdit { background-color: rgb(11, 53, 89); color: rgb(255, 255, 255); } /* QCPGraph 样式设置 */ #widgetChannel { background-color: rgb(10, 30, 45); } 这是我的qss代码,我的创建打开工程栏的框架中带有字体的背景颜色是白色的,我想把他修改成和不带字体的背景颜色一样应该如何修改
最新发布
11-07
<think>我们面对的问题是:如何修改QSS代码,使得“创建打开工程栏框架”中带字体的部分背景颜色与不带字体部分的背景颜色一致。 根据用户描述,已有完整的QSS代码,但用户没有粘贴上来。因此,我们需要给出通用的修改方法。 分析: 1. 目标区域由两部分组成:一部分有字体(文本),另一部分没有字体(可能是空白区域或图标等)。 2. 当前有字体的部分的背景色可能与无字体部分的背景色不同,需要将它们设置为相同。 思路: 1. 首先,我们需要确定这个“创建打开工程栏框架”对应的控件类型和选择器路径。 2. 然后,我们需要定位到有字体部分和无字体部分的具体控件或子控件。 3. 通过设置正确的选择器和背景颜色属性,使两部分的背景颜色一致。 假设: 通常,这样的框架可能是一个QFrame或者QWidget,内部包含一些QLabel(有字体)和其他的空白区域(可能是另一个QWidget或QLabel没有文本)。 具体步骤: 1. 查找无字体部分的背景颜色值。我们可以通过检查无字体部分的样式来确定当前背景颜色值。例如,如果无字体部分是一个QWidget,其背景颜色在QSS中可能是这样设置的: ``` QWidget#widgetName { background-color: #f0f0f0; } ``` 或者通过父控件继承而来。 2. 将有字体部分(比如QLabel)的背景颜色设置为同样的颜色值。 示例: 假设我们有一个框架(QFrame)的objectName为"projectFrame",它内部包含一个QLabel(用于显示文字)和一个QWidget(作为空白区域)。我们可以这样设置: 原始QSS可能类似: ```css /* 框架样式 */ #projectFrame { background-color: #e0e0e0; /* 整个框架的背景色 */ } /* 空白区域(可能是框架内的一个子控件) */ #projectFrame > QWidget#blankArea { background-color: #e0e0e0; /* 无字体部分的背景色 */ } /* 标签区域 */ #projectFrame > QLabel { background-color: white; /* 有字体部分的背景色,现在需要改为#e0e0e0 */ } ``` 修改:将QLabel的背景颜色改为和无字体部分一致(#e0e0e0): ```css #projectFrame > QLabel { background-color: #e0e0e0; } ``` 但是,有时候背景颜色可能是从父控件继承的,所以也可以考虑设置背景颜色为透明,这样就会显示父控件的背景色: ```css #projectFrame > QLabel { background-color: transparent; } ``` 注意:如果标签有边框或者内边距,可能需要调整。另外,如果标签的背景色被其他更具体的选择器覆盖,则需要调整选择器的优先级。 通用建议: 1. 使用更具体的选择器(如使用objectName)来确保覆盖。 2. 使用调试工具(如Qt Designer的样式编辑器或运行时使用Qt助手)检查实际应用的样式。 如果没有效果,检查: 1. 选择器是否正确?是否覆盖了其他样式? 2. 是否有更高优先级的选择器(如使用感叹号)设置了背景颜色? 3. 是否在代码中动态设置了样式? 由于用户没有提供完整的QSS代码,只能给出以上通用方法。如果用户能提供具体代码,我们可以给出更精确的修改。 相关问题: §§相关问题§§ 1. 如何查看Qt控件实际应用的样式? 2. 如何提高QSS选择器的优先级? 3. 在QSS中,背景颜色透明有哪些注意事项? </think>根据您的需求(将带字体部分的背景颜色调整为与无字体部分一致),结合引用[1]和[2]提到的QSS特性,以下是解决方案: ### 修改步骤 ```qss /* 原始框架样式 - 假设选择器为 #projectFrame */ #projectFrame { background-color: #f0f0f0; /* 无字体区域背景色 */ border-radius: 4px; padding: 10px; } /* 新增:针对带字体文本的子控件 */ #projectFrame QLabel, /* 所有标签文本 */ #projectFrame QAbstractButton, /* 按钮文本 */ #projectFrame QComboBox { /* 下拉框文本 */ background-color: transparent; /* 关键修改:背景透明继承父容器 */ color: #333; /* 保持字体颜色不变 */ padding: 5px; } ``` ### 关键修改说明 1. **透明背景**:使用 `background-color: transparent` 让文字区域透明,继承父容器背景色[^1] 2. **精确选择器**:通过层级选择器 `#parent > child` 精准定位文本控件 3. **保留文字样式**:单独设置 `color` 属性确保文字颜色不变 ### 注意事项 1. **选择器优先级**:若无效可能是选择器优先级不够,可尝试: ```qss #projectFrame > QLabel { background-color: transparent !important; } ``` 2. **控件类型覆盖**:确保覆盖所有文本控件类型(QLabel/QPushButton等) 3. **继承检查**:通过Qt Designer的 **属性编辑器** 检查实际应用的样式 ### 调试建议 若仍无效,可在main函数添加调试代码(引用[3]): ```cpp qDebug() << "Current stylesheet:" << qApp->styleSheet(); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

街 三 仔

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

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

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

打赏作者

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

抵扣说明:

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

余额充值