【问题标题】:PySide: How to set an SVG icon in QTreeWidget's item and change the size of the icon?PySide:如何在 QTreeWidget 的项目中设置 SVG 图标并更改图标的大小?
【发布时间】:2014-10-29 13:05:10
【问题描述】:

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

【问题讨论】:

    标签: qt python-2.7 svg pyside nuke


    【解决方案1】:

    好的,我找到了一个可行的解决方案。

    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

    【讨论】:

      猜你喜欢
      • 2021-04-18
      • 1970-01-01
      • 2018-05-04
      • 1970-01-01
      • 1970-01-01
      • 2019-11-10
      • 2015-11-17
      • 1970-01-01
      • 2021-12-24
      相关资源
      最近更新 更多