向 Qlistview 的项目添加图标
Adding icons to items of Qlistview
直奔主题,我想为 Qlistview
的项目添加图标。我一直在尝试 setData
,但没有成功。
代码:
import os
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
iconroot = os.path.dirname(__file__)
self.setFont(QtGui.QFont("Helvetica", 8, QtGui.QFont.Normal, italic=False))
lay = QtWidgets.QVBoxLayout(self)
self.listView = QtWidgets.QListView()
lay.addWidget(self.listView)
self.entry = QtGui.QStandardItemModel()
self.listView.setModel(self.entry)
self.listView.setSpacing(5)
for text in ("One", "two", "Three", "Four",
"Five etc.."):
it = QtGui.QStandardItem(text)
self.entry.appendRow(it)
it.setData(QtGui.QIcon(iconroot +'/images/flags'),QtCore.Qt.DecorationRole)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
想要这样的东西..
试一试:
import os
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
# iconroot = os.path.dirname(__file__)
self.setFont(QtGui.QFont("Helvetica", 8, QtGui.QFont.Normal, italic=False))
lay = QtWidgets.QVBoxLayout(self)
self.listView = QtWidgets.QListView()
lay.addWidget(self.listView)
self.entry = QtGui.QStandardItemModel()
self.listView.setModel(self.entry)
self.listView.setSpacing(5)
# for text in ("One", "two", "Three", "Four",
for i, text in enumerate(["One", "two", "Three", "Four", # +++
"Five etc.."]):
it = QtGui.QStandardItem(text)
self.entry.appendRow(it)
# it.setData(QtGui.QIcon(iconroot +'/images/flags'), QtCore.Qt.DecorationRole)
it.setData(QtGui.QIcon(os.path.join('images', 'bao{}.png'.format(i))), # +++
QtCore.Qt.DecorationRole)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
直奔主题,我想为 Qlistview
的项目添加图标。我一直在尝试 setData
,但没有成功。
代码:
import os
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
iconroot = os.path.dirname(__file__)
self.setFont(QtGui.QFont("Helvetica", 8, QtGui.QFont.Normal, italic=False))
lay = QtWidgets.QVBoxLayout(self)
self.listView = QtWidgets.QListView()
lay.addWidget(self.listView)
self.entry = QtGui.QStandardItemModel()
self.listView.setModel(self.entry)
self.listView.setSpacing(5)
for text in ("One", "two", "Three", "Four",
"Five etc.."):
it = QtGui.QStandardItem(text)
self.entry.appendRow(it)
it.setData(QtGui.QIcon(iconroot +'/images/flags'),QtCore.Qt.DecorationRole)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
想要这样的东西..
试一试:
import os
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
# iconroot = os.path.dirname(__file__)
self.setFont(QtGui.QFont("Helvetica", 8, QtGui.QFont.Normal, italic=False))
lay = QtWidgets.QVBoxLayout(self)
self.listView = QtWidgets.QListView()
lay.addWidget(self.listView)
self.entry = QtGui.QStandardItemModel()
self.listView.setModel(self.entry)
self.listView.setSpacing(5)
# for text in ("One", "two", "Three", "Four",
for i, text in enumerate(["One", "two", "Three", "Four", # +++
"Five etc.."]):
it = QtGui.QStandardItem(text)
self.entry.appendRow(it)
# it.setData(QtGui.QIcon(iconroot +'/images/flags'), QtCore.Qt.DecorationRole)
it.setData(QtGui.QIcon(os.path.join('images', 'bao{}.png'.format(i))), # +++
QtCore.Qt.DecorationRole)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())