Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 对Pyside中定义的模型使用QListView_Python_Qt_Pyside_Qlistview - Fatal编程技术网

Python 对Pyside中定义的模型使用QListView

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

我一直在尝试显示我使用PySide构建的列表。它不仅仅是一个字符串列表(或者我可以使用
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
可以与任何
角色一起使用