Python 对Pyside中定义的模型使用QListView
我一直在尝试显示我使用PySide构建的列表。它不仅仅是一个字符串列表(或者我可以使用Python 对Pyside中定义的模型使用QListView,python,qt,pyside,qlistview,Python,Qt,Pyside,Qlistview,我一直在尝试显示我使用PySide构建的列表。它不仅仅是一个字符串列表(或者我可以使用QListWidget),我还简化了它作为示例 from PySide import QtCore, QtGui class SimpleList(QtCore.QAbstractListModel): def __init__(self, contents): super(SimpleList, self).__init__() self.contents = con
QListWidget
),我还简化了它作为示例
from PySide import QtCore, QtGui
class SimpleList(QtCore.QAbstractListModel):
def __init__(self, contents):
super(SimpleList, self).__init__()
self.contents = contents
def rowCount(self, parent):
return len(self.contents)
def data(self, index, role):
return str(self.contents[index.row()])
app = QtGui.QApplication([])
contents = SimpleList(["A", "B", "C"]) # In real code, these are complex objects
simplelist = QtGui.QListView(None)
simplelist.setGeometry(QtCore.QRect(0, 10, 791, 391))
simplelist.setModel(contents)
simplelist.show()
app.exec_()
我明白了,只是一张空名单
我做错了什么?您应该检查
角色
参数:
def data(self, index, role):
if role == QtCore.Qt.DisplayRole:
return str(self.contents[index.row()])
但奇怪的是,QTableView
可以与任何角色一起使用