I am using qdarkstyle on my PyQt5 GUI, and I am trying to make a widget with internal combo boxes. The items in the combo box all have a square indicator to the left hand side that I want to hide.
I have tried setting the style sheet with no success
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;
}
"""
I was able to figure it out- it is solved with a QStyledItemDelegate, not a style sheet. First define the new delegate:
(Edited based on musicamante‘s reply)
from PyQt5.QtWidgets import QApplication, QComboBox, QStyledItemDelegate, QStyleOptionViewItem
from PyQt5.QtGui import QPainter
from PyQt5.QtCore import QModelIndex
class NoIconItemDelegate(QStyledItemDelegate):
def initStyleOption(self, option: QStyleOptionViewItem, index: QModelIndex):
option.decorationPosition = QStyleOptionViewItem.Position.Right
super().initStyleOption(option, index)
Then we can use this delegate for the combo box
self.cb.setItemDelegate(NoIconItemDelegate(self.cb.view()))