def paintEvent(self, event): '''Handle paint *event*.''' itemData = self.itemData(self.currentIndex()) if itemData: # Draw main control. painter = QtWidgets.QStylePainter(self) painter.setPen(self.palette().color(QtGui.QPalette.Text)) option = QtWidgets.QStyleOptionComboBox() self.initStyleOption(option) painter.drawComplexControl(QtWidgets.QStyle.CC_ComboBox, option) # Get QTextDocument from delegate to use for painting HTML text. delegate = self.itemDelegate() document = delegate.getTextDocument( option, self.itemData(self.currentIndex())) style = painter.style() # QtWidgets.QApplication.style() paint_context = QtGui.QAbstractTextDocumentLayout.PaintContext() text_rectangle = style.subElementRect( QtWidgets.QStyle.SE_ComboBoxFocusRect, option, self) painter.save() painter.translate(text_rectangle.topLeft()) painter.setClipRect( text_rectangle.translated(-text_rectangle.topLeft())) document.documentLayout().draw(painter, paint_context) painter.restore() else: super(HtmlComboBox, self).paintEvent(event)
def sizeHint(self): '''Return preferred size hint.''' option = QtWidgets.QStyleOptionComboBox() self.initStyleOption(option) data = self.itemData(self.currentIndex()) # Get QTextDocument from delegate to use for calculating size hint. delegate = self.itemDelegate() document = delegate.getTextDocument(option, data) # Adjust the size to fix issue occurring on windows. size = QtCore.QSize(document.idealWidth(), document.size().height() + 5) return size