def _add_tag(self, tag_name=None, tag_color=None): if not tag_name or not tag_color: dialog = CreateTagDialog(self) dialog.exec_() result = dialog.result() if result: tag_color = dialog._color tag_name = dialog._name else: return item = QtWidgets.QListWidgetItem() item.setText(tag_name) item.setData(QtCore.Qt.BackgroundRole, QtGui.QBrush(QtGui.QColor(tag_color))) self.tags_lw.addItem(item)
def _draw_background(self, painter, option, index): rect = deepcopy(option.rect) path = QtGui.QPainterPath() path.addRoundedRect(rect, 3, 3) if option.state & QtWidgets.QStyle.State_Selected: background_color = QtGui.QBrush( option.palette.color(QtGui.QPalette.Highlight)) else: background_color = index.data(QtCore.Qt.BackgroundRole) r, g, b, a = background_color.color().getRgb() color_luminance = TagDelegate.color_luminance(r, g, b) brightness = 50 if color_luminance > 150: brightness = -50 pen = QtGui.QPen( QtGui.QColor(*TagDelegate.color_variant( background_color.color().name(), brightness))) painter.setPen(pen) painter.fillPath(path, background_color) painter.drawPath(path)
painter.drawPath(path) def _draw_text(self, painter, option, index): rect = deepcopy(option.rect) name = index.data(QtCore.Qt.DisplayRole) # pen = QtGui.QPen(QtCore.Qt.black) # painter.setPen(pen) painter.drawText(rect, QtCore.Qt.AlignHCenter | QtCore.Qt.AlignVCenter, name) def sizeHint(self, option, index): name = index.data(QtCore.Qt.DisplayRole) return QtCore.QSize(option.fontMetrics.width(name) + 8, 21) if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) listwidget = QtWidgets.QListWidget() listwidget.setFlow(QtWidgets.QListView.LeftToRight) listwidget.setSpacing(3) listwidget.setItemDelegate(TagDelegate()) for x in xrange(5): item = QtWidgets.QListWidgetItem() item.setText("Item number {}".format(x)) item.setData(QtCore.Qt.BackgroundRole, QtGui.QBrush(QtGui.QColor("#444444"))) listwidget.addItem(item) listwidget.show() app.exec_()