介绍PyQt5中如何修改复选框中未被选中的指标的背景颜色,主要涉及到QProxyStyle和QStyleOptionViewItem两个类。步骤如下:
1.创建一个自定义类,继承自QProxyStyle,并且重写drawPrimitive方法:
from PyQt5.QtWidgets import QProxyStyle
from PyQt5.QtGui import QPainter, QColor, QPalette, QBrush, QPen
from PyQt5.QtCore import Qt
class CheckboxStyle(QProxyStyle):
def drawPrimitive(self, element, option, painter, widget=None):
if element == QStyle.PE_IndicatorCheckBox:
checked = option.state & QStyle.State_On
palette = option.palette
if not checked and not option.state & QStyle.State_NoChange:
new_palette = QPalette(palette)
new_palette.setColor(QPalette.Window, QColor(255, 0, 0)) # 未选中时显示为红色
option = QStyleOptionViewItem(option)
option.palette = new_palette
super().drawPrimitive(element, option, painter, widget)
2.创建QTreeView控件,并将它的样式设置为自定义类
from PyQt5.QtWidgets import QTreeView, QApplication
from PyQt5.QtGui import QStandardItemModel, QStandardItem
app = QApplication([])
treeview = QTreeView()
model = QStandardItemModel()
rootNode = model.invisibleRootItem()
node1 = QStandardItem("Node 1")
node2 = QStandardItem("Node 2")
rootNode.appendRow(node1)
rootNode.appendRow(node2)
treeview.setModel(model)
treeview.setStyle(CheckboxStyle()) # 设置样式
treeview.show()
app.exec_()
示例1:将复选框未选中时的背景颜色设置为红色
class RedCheckBoxStyle(QProxyStyle):
def drawPrimitive(self, element, option, painter, widget=None):
if element == QStyle.PE_IndicatorCheckBox:
checked = option.state & QStyle.State_On
palette = option.palette
if not checked and not option.state & QStyle.State_NoChange:
new_palette = QPalette(palette)
new_palette.setColor(QPalette.Window, QColor(255, 0, 0)) # 未选中时显示为红色
option = QStyleOptionViewItem(option)
option.palette = new_palette
super().drawPrimitive(element, option, painter, widget)
if __name__ == '__main__':
app = QApplication([])
treeview = QTreeView()
model = QStandardItemModel()
rootNode = model.invisibleRootItem()
node1 = QStandardItem("Node 1")
node2 = QStandardItem("Node 2")
rootNode.appendRow(node1)
rootNode.appendRow(node2)
treeview.setModel(model)
treeview.setStyle(RedCheckBoxStyle()) # 设置样式
treeview.show()
app.exec_()
示例2:将复选框未选中时的背景颜色设置为蓝色
class BlueCheckBoxStyle(QProxyStyle):
def drawPrimitive(self, element, option, painter, widget=None):
if element == QStyle.PE_IndicatorCheckBox:
checked = option.state & QStyle.State_On
palette = option.palette
if not checked and not option.state & QStyle.State_NoChange:
new_palette = QPalette(palette)
new_palette.setColor(QPalette.Window, QColor(0, 0, 255)) # 未选中时显示为蓝色
option = QStyleOptionViewItem(option)
option.palette = new_palette
super().drawPrimitive(element, option, painter, widget)
if __name__ == '__main__':
app = QApplication([])
treeview = QTreeView()
model = QStandardItemModel()
rootNode = model.invisibleRootItem()
node1 = QStandardItem("Node 1")
node2 = QStandardItem("Node 2")
rootNode.appendRow(node1)
rootNode.appendRow(node2)
treeview.setModel(model)
treeview.setStyle(BlueCheckBoxStyle()) # 设置样式
treeview.show()
app.exec_()
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:PyQt5 – 复选框中未被选中的指标的背景颜色 - Python技术站