python - PyQt4 QAbstractListModel - 仅显示第一个数据小部件

标签 python python-2.7 user-interface pyqt4 model-view

我正在尝试实现 QAbstractListModel 类以显示几个类似的小部件。 以下代码显示了我的问题:

import sys
from PyQt4 import QtCore
from PyQt4 import QtGui

class Model(QtCore.QAbstractListModel):

    def __init__(self, parent=None):
        super(QtCore.QAbstractListModel, self).__init__(parent)
        self._widgets = []


    def headerData(self, section, orientation, role):
        """ Returns header for columns """
        return "bla"


    def rowCount(self, parentIndex=QtCore.QModelIndex()):
        """ Returns number of interfaces """
        return len(self._widgets)


    def data(self, index, role):
        """ Returns the data to be displayed """
        if role == QtCore.Qt.DisplayRole:
            row = index.row()
            return self._widgets[row]


    def insertRow(self, widget, parentIndex=QtCore.QModelIndex()):
        """ Inserts a row into the model """
        self.beginInsertRows(parentIndex, 0, 1)
        self._widgets.append(widget)
        self.endInsertRows()


class Widget(QtGui.QWidget):

    def __init__(self, parent=None, name="None"):
        super(QtGui.QWidget, self).__init__(parent)
        self.layout = QtGui.QHBoxLayout()
        self.setLayout(self.layout)
        self.checkbox = QtGui.QCheckBox()
        self.button = QtGui.QPushButton(self)
        self.label = QtGui.QLabel(self)
        self.label.setText(name)
        self.layout.addWidget(self.checkbox)
        self.layout.addWidget(self.button)
        self.layout.addWidget(self.label)

class Window(QtGui.QMainWindow):

    def __init__(self, parent=None):
        super(QtGui.QMainWindow, self).__init__(parent)
        self.view = QtGui.QListView(self)
        self.model = Model()
        self.view.setModel(self.model)
        self.setCentralWidget(self.view)

        self.model.insertRow(
            widget=Widget(self)
        )
        self.model.insertRow(
            widget=Widget(self)
        )
        self.model.insertRow(
            widget=Widget(self)
        )
        self.model.insertRow(
            widget=Widget(self)
        )

        self.show()


app = QtGui.QApplication(sys.argv)
window = Window()
sys.exit(app.exec_())

它有效,列表中有四个可点击的条目,但实际上只有其中一个条目显示了小部件。这是为什么?

我猜这种行为是由 data() 或我使用 beginInsertRows() 引起的,但我无法弄清楚错误在哪里。

<小时/>

ìnsertRow() 函数现在看起来像这样

def insertRow(self, widget, parentIndex=QtCore.QModelIndex()):
    """ Inserts a row into the model """
    self.beginInsertRows(parentIndex, len(self._widgets), len(self._widgets))
    self._widgets.append(widget)
    self.endInsertRows()

但是还是不行。

最佳答案

实际上,显示了四个小部件。问题是,它们都显示在左上角。如果您输入名称,您可以看到它们重叠,此处为“1”、“2”、“3”、“4”:

enter image description here

修复行号并不能解决问题。小部件将位于正确的行中,但仍显示在左上角。这是因为 data 应该是 return text for the display role

要显示简单的小部件,我建议使用 QListWidgetsetItemWidget方法。否则你将不得不使用委托(delegate)。

关于python - PyQt4 QAbstractListModel - 仅显示第一个数据小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34134663/

相关文章:

python - 在没有安装库的情况下运行 python 脚本

python - 用 C 扩展 Python : Convert PyStringObject to PyIntObject

python - 减少 py2exe 分布大小,同时包含一些 numpy 函数

c# - 在 MonoTouch 中获取没有 UI 的 iOS 屏幕分辨率

java - Java 中的 HMI 编辑器

Python 子进程不会终止

python - 为什么python中的显式协议(protocol)不需要@runtimechecks的属性实现,而隐式协议(protocol)呢?

python - pip install 默认带有删除选项

python - 如何从代码而不是 Google App Engine 上的文件呈现 Django 模板

java - native 操作系统外观 Netbeans 不能与 UIManager.setLookAndFeel(nativeLF) 一起使用;