Issue
Well as the title already tells, I want to bold the placeholder text of a QComboBox
.
I can't see a way to bold the placeholder of a QComboBox
. With QFont
i can bold every item in the QComboBox
, but I just want the placeholder being bold. setPlaceholderText()
takes only one argument for the text itself.
My current code:
comboBox = QComboBox()
# - Bold whole combobox-
# font = QFont()
# font.setBold(True)
# comboBox.setFont(font)
comboBox.addItems(items)
comboBox.setPlaceholderText("Select text")
comboBox.setCurrentIndex(-1)
Thanks in advance.
Solution
Solution
Due of a bug in the library, QComboBox
has to be overwritten in order to set a placeholder. I already had the class for overwriting QComboBox
so I did a minor change to make the placeholderText
bold.
class QComboBox(QComboBox):
def __init__(self, boldPlaceholder: bool = False, *args, **kwargs):
super().__init__(*args, **kwargs)
self.boldPlaceholder = boldPlaceholder
def paintEvent(self, event):
painter = QStylePainter(self)
painter.setPen(self.palette().color(QPalette.Text))
# draw the combobox frame, focusrect and selected etc.
opt = QStyleOptionComboBox()
self.initStyleOption(opt)
painter.drawComplexControl(QStyle.CC_ComboBox, opt)
if self.currentIndex() < 0:
opt.palette.setBrush(
QPalette.ButtonText,
opt.palette.brush(QPalette.ButtonText).color().lighter()
)
if self.placeholderText():
opt.currentText = self.placeholderText()
# bold placeholdertText
if self.boldPlaceholder and self.placeholderText():
font = self.font()
font.setBold(True)
painter.setFont(font)
# draw the icon and text
painter.drawControl(QStyle.CE_ComboBoxLabel, opt)
Answered By - puncher
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.