首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用QItemDelegate绘制图标

使用QItemDelegate绘制图标
EN

Stack Overflow用户
提问于 2019-01-11 20:29:50
回答 1查看 924关注 0票数 0

我想在单列中绘制第二个图标,在该列的标准绘制事件的右侧。你可以看到我在第三列的最右边画了一个红色的图标,我该如何使用QItemDelegate来完成这个任务呢?我更喜欢默认的绘画,即使仍然发生,以痛苦的装饰角色和显示角色文本。

代码语言:javascript
复制
import os, sys, pprint
from Qt import QtGui, QtWidgets, QtCore


class Window(QtWidgets.QDialog):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)
        self.resize(500, 400)

        self.uiItems = QtWidgets.QTreeView()
        self.uiItems.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers)
        self.uiItems.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows)
        self.uiItems.setSelectionMode(QtWidgets.QAbstractItemView.ExtendedSelection)
        self.uiItems.setModel(QtGui.QStandardItemModel())
        self.uiItems.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)

        self.layout = QtWidgets.QVBoxLayout()
        self.layout.addWidget(self.uiItems)
        self.setLayout(self.layout)

        for i in range(3):
            parent1 = QtGui.QStandardItem('Parente Item ' + str(i))
            parent2 = QtGui.QStandardItem()
            parent3 = QtGui.QStandardItem()
            self.uiItems.model().appendRow([parent1, parent2, parent3])

            for x in range(3):
                col1 = QtGui.QStandardItem('Child Item' + str(x))
                col2 = QtGui.QStandardItem('Item' + str(x))
                col3 = QtGui.QStandardItem('Item' + str(x))
                col3.setData(self.createDotPixmap(), role=QtCore.Qt.DecorationRole)
                parent1.appendRow([col1,col2,col3])

        self.uiItems.expandAll()


    def createRectPixmap(self, col=QtGui.QColor(240,50,50)):
        px = QtGui.QPixmap(12,12)
        px.fill(QtCore.Qt.transparent)
        pxSize = px.rect().adjusted(1,1,-1,-1)
        painter = QtGui.QPainter(px)
        painter.setRenderHint(QtGui.QPainter.Antialiasing)
        painter.setBrush(col)
        painter.setPen(QtGui.QPen(QtGui.QColor(150,20,20), 1.25))
        painter.drawRect(pxSize)
        painter.end()
        return px


    def createDotPixmap(self, col=QtGui.QColor(128,128,128)):
        px = QtGui.QPixmap(12,12)
        px.fill(QtCore.Qt.transparent)
        pxSize = px.rect().adjusted(1,1,-1,-1)
        painter = QtGui.QPainter(px)
        painter.setRenderHint(QtGui.QPainter.Antialiasing)
        painter.setBrush(col)
        painter.setPen(QtGui.QPen(QtGui.QColor(15,15,15), 1.25))
        painter.drawEllipse(pxSize)
        painter.end()
        return px




if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    ex = Window()
    ex.show()
    app.exec_()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-01-11 23:26:46

解决方案是:

代码语言:javascript
复制
import os, sys, pprint
from Qt import QtGui, QtWidgets, QtCore

DecorationRole2 = QtCore.Qt.UserRole + 1000

class IconDelegate(QtWidgets.QStyledItemDelegate):
    def paint(self, painter, option, index):
        super(self.__class__, self).paint(painter, option, index)
        value = index.data(DecorationRole2)
        if value:
            margin = 10
            mode = QtGui.QIcon.Normal

            if not (option.state & QtWidgets.QStyle.State_Enabled):
                mode = QtGui.QIcon.Disabled
            elif option.state & QtWidgets.QStyle.State_Selected:
                mode = QtGui.QIcon.Selected

            if isinstance(value, QtGui.QPixmap):
                icon = QtGui.QIcon(value)
                option.decorationSize = value.size() / value.devicePixelRatio()

            elif isinstance(value, QtGui.QColor):
                pixmap = QtGui.QPixmap(option.decorationSize)
                pixmap.fill(value)
                icon = QtGui.QIcon(pixmap)

            elif isinstance(value, QtGui.Image):
                icon = QtGui.QIcon(QtGui.QPixmap.fromImage(value))
                option.decorationSize = value.size() / value.devicePixelRatio()

            elif isinstance(value, QtGui.QIcon):
                state =  QtGui.QIcon.On if option.state & QtWidgets.QStyle.State_Open else QtGui.QIcon.Off
                actualSize = option.icon.actualSize(option.decorationSize, mode, state)
                option.decorationSize = QtCore.QSize(min(option.decorationSize.width(), actualSize.width()), min(option.decorationSize.height(), actualSize.height()))

            r = QtCore.QRect(QtCore.QPoint(), option.decorationSize)
            r.moveCenter(option.rect.center())
            r.setRight(option.rect.right() - margin)
            state = QtGui.QIcon.On if option.state & QtWidgets.QStyle.State_Open else QtGui.QIcon.Off
            icon.paint(painter, r, QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter, mode, state)

class Window(QtWidgets.QDialog):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)
        self.resize(500, 400)

        self.uiItems = QtWidgets.QTreeView()
        self.uiItems.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers)
        self.uiItems.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows)
        self.uiItems.setSelectionMode(QtWidgets.QAbstractItemView.ExtendedSelection)
        self.uiItems.setModel(QtGui.QStandardItemModel())
        self.uiItems.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)

        delegate = IconDelegate(self.uiItems)
        self.uiItems.setItemDelegateForColumn(2, delegate)

        self.layout = QtWidgets.QVBoxLayout()
        self.layout.addWidget(self.uiItems)
        self.setLayout(self.layout)

        for i in range(3):
            parent1 = QtGui.QStandardItem('Parente Item ' + str(i))
            parent2 = QtGui.QStandardItem()
            parent3 = QtGui.QStandardItem()
            self.uiItems.model().appendRow([parent1, parent2, parent3])

            for x in range(3):
                col1 = QtGui.QStandardItem('Child Item' + str(x))
                col2 = QtGui.QStandardItem('Item' + str(x))
                col3 = QtGui.QStandardItem('Item' + str(x))
                col3.setData(self.createDotPixmap(), role=QtCore.Qt.DecorationRole)
                col3.setData(self.createRectPixmap(), role=DecorationRole2)
                parent1.appendRow([col1,col2,col3])
        self.uiItems.expandAll()

    def createRectPixmap(self, col=QtGui.QColor(240,50,50)):
        px = QtGui.QPixmap(12,12)
        px.fill(QtCore.Qt.transparent)
        pxSize = px.rect().adjusted(1,1,-1,-1)
        painter = QtGui.QPainter(px)
        painter.setRenderHint(QtGui.QPainter.Antialiasing)
        painter.setBrush(col)
        painter.setPen(QtGui.QPen(QtGui.QColor(150,20,20), 1.25))
        painter.drawRect(pxSize)
        painter.end()
        return px


    def createDotPixmap(self, col=QtGui.QColor(128,128,128)):
        px = QtGui.QPixmap(12,12)
        px.fill(QtCore.Qt.transparent)
        pxSize = px.rect().adjusted(1,1,-1,-1)
        painter = QtGui.QPainter(px)
        painter.setRenderHint(QtGui.QPainter.Antialiasing)
        painter.setBrush(col)
        painter.setPen(QtGui.QPen(QtGui.QColor(15,15,15), 1.25))
        painter.drawEllipse(pxSize)
        painter.end()
        return px

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    ex = Window()
    ex.show()
    app.exec_()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54146605

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档