PySide:如何在QTreeWidget项目中设置SVG图标并更改图标的大小?

时间:2014-09-04 17:08:28

标签: qt python-2.7 svg pyside nuke

我有一个在Nuke 8中使用SVG图标的UI。虽然使用SVG文件设置图标工作正常,我似乎无法找到一种方法将图标的大小更改为Qt中更大的尺寸。我已经尝试设置图标的大小,然后加载SVG文件,在窗口小部件上设置图标,然后将窗口小部件放在QTreeWidgetItem中,并设置窗口小部件的样式表以使用SVG。我知道我可以在加载文件之前扩展SVG文件,但我更喜欢在Qt中处理它。

以下是我正在做的一些示例代码(这样可行,但图标无法缩放。):

from PySide import QtGui, QtCore

class TestTreeItem(QtGui.QTreeWidgetItem):
    def __init__(self, parent, value, status):
        super(TestTreeItem, self).__init__(parent)

        # Column 0
        self.setText(value)

        # Column 1
        # Using Qt Resources. All of the maps are to svg files.
        status_map = {0: ":/images/good",
        1: ":/images/warning",
        2: ":/images/error"}

        self.setSizeHint(1, QtCore.QSize(64, 64))
        icon = QtGui.QIcon(status_map[status])
        self.setIcon(1, icon)

无效的代码:

设置图标大小,然后加载图标

from PySide import QtGui, QtCore

class TestTreeItem(QtGui.QTreeWidgetItem):
    def __init__(self, parent, value, status):
        super(TestTreeItem, self).__init__(parent)

        # Column 0
        self.setText(value)

        # Column 1
        # Using Qt Resources. All of the maps are to svg files.
        status_map = {0: ":/images/good",
        1: ":/images/warning",
        2: ":/images/error"}

        self.setSizeHint(1, QtCore.QSize(64, 64))
        icon = QtGui.QIcon(QtGui.QSize(64, 64))
        icon.addFile(status_map[status])
        self.setIcon(1, icon)

创建像素图并将其缩放

from PySide import QtGui, QtCore

class TestTreeItem(QtGui.QTreeWidgetItem):
    def __init__(self, parent, value, status):
        super(TestTreeItem, self).__init__(parent)

        # Column 0
        self.setText(value)

        # Column 1
        # Using Qt Resources. All of the maps are to svg files.
        status_map = {0: ":/images/good",
        1: ":/images/warning",
        2: ":/images/error"}

        self.setSizeHint(1, QtCore.QSize(64, 64))
        pixmap = QtGui.QPixmap(status_map[status])
        pixmap.scaled(64, 64)
        icon = QtGui.QIcon(pixmap)
        self.setIcon(1, icon)

创建一个空像素图,​​然后加载SVG

from PySide import QtGui, QtCore

class TestTreeItem(QtGui.QTreeWidgetItem):
    def __init__(self, parent, value, status):
        super(TestTreeItem, self).__init__(parent)

        # Column 0
        self.setText(value)

        # Column 1
        # Using Qt Resources. All of the maps are to svg files.
        status_map = {0: ":/images/good",
        1: ":/images/warning",
        2: ":/images/error"}

        self.setSizeHint(1, QtCore.QSize(64, 64))
        pixmap = QtGui.QPixmap(QtGui.QSize(64, 64))
        pixmap.load(status_map[status])
        icon = QtGui.QIcon(pixmap)
        self.setIcon(1, icon)

创建一个小部件,然后设置样式表以使用SVG

from PySide import QtGui, QtCore

class TestTreeItem(QtGui.QTreeWidgetItem):
    def __init__(self, parent, value, status):
        super(TestTreeItem, self).__init__(parent)

        # Column 0
        self.setText(value)

        # Column 1
        # Using Qt Resources. All of the maps are to svg files.
        widget = QtGui.QWidget(parent)
        status_map = {0: ":/images/good",
        1: ":/images/warning",
        2: ":/images/error"}

        self.setSizeHint(1, QtCore.QSize(64, 64))
        widget.setStyleSheet("image: url({});".format(status_map[status]))
        parent.setItemWidget(self, 1, widget)

1 个答案:

答案 0 :(得分:3)

好吧,我找到了一个有效的解决方案。

from PySide import QtGui, QtCore, QtSvg

class TestTreeItem(QtGui.QTreeWidgetItem):
    def __init__(self, parent, value, status):
        super(TestTreeItem, self).__init__(parent)

        # Column 0
        self.setText(value)

        # Column 1
        # Using Qt Resources. All of the maps are to svg files.
        status_map = {0: ":/images/good",
        1: ":/images/warning",
        2: ":/images/error"}

        svg_renderer = QtSvg.QSvgRenderer(status_map[status])
        image = QtGui.QImage(64, 64, QtGui.QImage.Format_ARGB32)
        # Set the ARGB to 0 to prevent rendering artifacts
        image.fill(0x00000000)
        svg_renderer.render(QtGui.QPainter(image))
        pixmap = QtGui.QPixmap.fromImage(image)
        icon = QtGui.QIcon(pixmap)
        self.setIcon(1, icon)

        self.setSizeHint(1, QtCore.QSize(64, 64))

这是基于这个答案:https://stackoverflow.com/a/8551810/3108288