简体   繁体   English

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

[英]PyQt4 QAbstractListModel - Only first data widget is displayed

I'm trying to implement the QAbstractListModel class in order to display several similar widgets. 我正在尝试实现QAbstractListModel类,以显示几个类似的小部件。 The following code shows my problem: 以下代码显示了我的问题:

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_())

It works, there are four clickable entries in the list, but in only one of them the widget is actually displayed. 它可以正常工作,列表中有四个可单击的条目,但实际上只有其中一个显示了小部件。 Why is that? 这是为什么?

I guess this behaviour is either caused by data() or how I'm using beginInsertRows() , but I can't figure out where the error is. 我猜这可能是由data()引起的,还是由我使用beginInsertRows() ,但我无法弄清楚错误在哪里。


ìnsertRow() function now looks like that ì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()

but it still does not work. 但它仍然不起作用。

self.beginInsertRows(parentIndex, 0, 1)

0 - start 1 - end 0-开始1-结束

You refresh only first row during insert 您在插入过程中仅刷新第一行

self.beginInsertRows(parentIndex, len(self._widgets), len(self._widgets))

must be work. 必须工作。 Don't remember this method 不记得这种方法

self.model.reset()

this refresh all list, without specific rows 此刷新所有列表,没有特定的行

Actually, the four widgets are displayed. 实际上,将显示四个小部件。 The issue is, they're all displayed in the top left corner. 问题是,它们都显示在左上角。 You can see they overlap if you put names, here "1", "2", "3", "4": 如果输入名称,则可以看到它们重叠,这里是“ 1”,“ 2”,“ 3”,“ 4”:

在此处输入图片说明

Fixing the row numbers doesn't solve the issue. 固定行号不能解决问题。 The widgets will be in the correct rows but will still be displayed on the top left. 窗口小部件将位于正确的行中,但仍显示在左上方。 It is because data is supposed to return text for the display role . 这是因为data应该返回显示角色的文本

To display simple widgets, I suggest using QListWidget and the setItemWidget method. 要显示简单的小部件,建议使用QListWidgetsetItemWidget方法。 Otherwise you'll have to use delegates. 否则,您将不得不使用委托。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM