class HtmlComboBox(QComboBox): def __init__(self, state, parent=None): super().__init__(parent) self.state = state self.updateDisplayFonts() self.label = QLabel() self.label.setTextFormat(Qt.RichText) self.setEditable(False) self.setItemDelegate(HtmlDelegate()) def updateDisplayFonts(self): self.setFont(QFont(self.state.stdFontFamily, self.state.stdFontSize)) def minimumSizeHint(self): return self.sizeHint() def sizeHint(self): size = super().minimumSizeHint() width = 0 for i in range(self.count()): text = Lib.htmlToPlainText(self.itemText(i)) w = self.fontMetrics().width(text) if w > width: width = w width += self.fontMetrics().width("W" * 3) return QSize(width, size.height()) def paintEvent(self, event): painter = QStylePainter(self) painter.setPen(self.palette().color(QPalette.Text)) # Draw the combobox frame, focus rect, selected etc. opt = QStyleOptionComboBox() self.initStyleOption(opt) opt.currentText = "" # Don't draw the raw HTML painter.drawComplexControl(QStyle.CC_ComboBox, opt) # Draw the icon and text painter.drawControl(QStyle.CE_ComboBoxLabel, opt) # Draw the HTML self.label.setText(self.currentText()) self.label.adjustSize() pixmap = QPixmap(self.label.width(), self.label.height()) pixmap.fill(Qt.transparent) self.label.render(pixmap, renderFlags=QWidget.RenderFlags(0)) rect = QRect(opt.rect) y = (rect.height() - self.label.height()) / 2 rect.setX(self.fontMetrics().width("n")) rect.setY(y) rect.setHeight(pixmap.height()) rect.setWidth(pixmap.width()) painter.drawPixmap(rect, pixmap, pixmap.rect())
class MyLable(QWidget): """ a widget contains a picture and two line of text """ def __init__(self, title, subtitle, icon_path): """ :param title: str title :param subtitle: str subtitle :param icon_path: path of picture """ super(MyLable, self).__init__() self.lb_title = QLabel(title) self.lb_title.setFont(QFont("Arial", 10, QFont.Bold)) self.lb_subtitle = QLabel(subtitle) self.lb_subtitle.setFont(QFont("Arial", 8, QFont.StyleItalic)) self.lb_subtitle1 = QLabel(subtitle) self.lb_subtitle1.setFont(QFont("Arial", 8, QFont.StyleItalic)) self.lb_icon = QLabel() self.lb_icon.setFixedSize(40, 40) pixMap = QPixmap(icon_path).scaled(self.lb_icon.width(), self.lb_icon.height()) self.lb_icon.setPixmap(pixMap) self.lb_icon1 = QLabel() self.lb_icon1.setFixedSize(40, 40) pixMap1 = QPixmap(icon_path).scaled(self.lb_icon.width(), self.lb_icon.height()) self.lb_icon1.setPixmap(pixMap1) self.double_click_fun = None self.init_ui() def init_ui(self): """handle layout""" ly_main = QHBoxLayout() ly_right = QVBoxLayout() ly_right.addWidget(self.lb_title) ly_right.addWidget(self.lb_subtitle) ly_right.addWidget(self.lb_subtitle1) ly_right.setAlignment(Qt.AlignVCenter) ly_main.addWidget(self.lb_icon) ly_main.addLayout(ly_right) ly_main.addWidget(self.lb_icon1) self.setLayout(ly_main) self.resize(90, 60) def get_lb_title(self): return self.lb_title.text() def get_lb_subtitle(self): return self.lb_subtitle.text()