我有一个在 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)
最佳答案
好吧,我找到了一个有效的解决方案。
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))
关于qt - PySide:如何在QTreeWidget的项目中设置SVG图标并更改图标的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25671275/