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