我在 PyQt5 GUI 上使用 qdarkstyle,我正在尝试制作一个带有内部组合框的小部件。组合框中的项目在左侧都有一个我想隐藏的方形指示器。
我尝试设置样式表但没有成功
class Panel(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.layout = QVBoxLayout()
self.cb = QComboBox()
self.cb.addItems(["hello", "world"])
self.cb.setStyleSheet(self.getcbstylesheet())
self.layout.addWidget(self.cb)
self.setLayout(self.layout)
def getcbstylesheet(self):
return """
QComboBox::indicator {
background-color: transparent;
selection-background-color: transparent;
color: transparent;
selection-color: transparent;
}
"""
我能够弄清楚它 - 它是用 QStyledItemDelegate 解决的,而不是样式表。首先定义新的委托:
from PyQt5.QtWidgets import QApplication, QComboBox, QStyledItemDelegate
from PyQt5.QtGui import QPainter
from PyQt5.QtCore import QModelIndex, QStyleOptionViewItem
class NoIconItemDelegate(QStyledItemDelegate):
def paint(self, painter: QPainter, option: QStyleOptionViewItem, index: QModelIndex):
option.decorationPosition = QStyleOptionViewItem.Position.Right
super().paint(painter, option, index)
然后我们可以将此委托用于组合框
self.cb.setItemDelegate(NoIconItemDelegate())