【问题标题】:Using QListView with a model defined in Pyside将 QListView 与 Pyside 中定义的模型一起使用
【发布时间】:2012-02-17 23:52:35
【问题描述】:

我一直在尝试显示我使用 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_()

我看到nothing,只是一个空列表。

我做错了什么?

【问题讨论】:

    标签: python qt pyside qlistview


    【解决方案1】:

    你应该检查role参数:

    def data(self, index, role):
        if role == QtCore.Qt.DisplayRole:
            return str(self.contents[index.row()])
    

    但很奇怪,QTableView 可以与任何role 一起使用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多