1

我有一个在 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)
4

1 回答 1

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

于 2014-09-05T15:57:43.833 回答