PyQt5: Invisible items in a QML ListView (perhaps a bug?)
-
I recently encountered a strange behaviour in the QML ListView Component.
When I was subclassing QAbstractListModel to provide a custom model to a QML ListView, suddenly only the first item in the ListView was visible. All the other items were invisible, but they were still there and took up space, I could scroll down the ListView.
I created a little example that reproduces the problem for me:Python file
@# -- coding: utf-8 --
import sys
from PyQt5.QtCore import QAbstractListModel, Qt
from PyQt5.QtWidgets import QApplication
from PyQt5.QtQml import QQmlApplicationEnginedef main():
app = QApplication(sys.argv)engine = QQmlApplicationEngine() engine.load("modeltest.qml") engine.rootObjects()[0].setProperty("model", TestModel()) app.exec()
class TestModel(QAbstractListModel):
def init(self):
super().init()
self.data = ["a", "b", "c"]def rowCount(self, QModelIndex_parent=None, *args, **kwargs): return len(self.data) def data(self, QModelIndex, int_role=None): return self.data[QModelIndex.row()] def roleNames(self): return {Qt.UserRole + 1: "test"}
if name == "main":
main()
@QML file
@
import QtQuick 2.0
import QtQuick.Controls 1.2ApplicationWindow {
id: app
visible: true
width: 400
height: 700
property alias model: list.modelListView { id: list anchors.fill: parent focus: true delegate: Rectangle { height: 100 width: parent.width color: "lightgreen" visible: true Text { anchors.centerIn: parent text: model.test } } }
}
@My setup:
Arch Linux
Qt 5.4.0
PyQt 5.4.0
Python 3.4.2Edit:
here is a little screencast where you can see the problem: http://gfycat.com/OffensiveShinyHagfish
-
Can anybody reproduce this problem with their own setup?
-
I can reproduce your error. I can't explain where your mistake is but it works if I do the following in the Python file:
def main(): app = QApplication(sys.argv) engine = QQmlApplicationEngine() context = engine.rootContext() model = TestModel() context.setContextProperty('testModel', model) engine.load("modeltest.qml") sys.exit(app.exec_())
-
I too face the same error when using PyQt5. When I inherit QAbstract model and create my own model and send an instance of it to QML using pyqtsignal and then assign this to the model property of the ListView in QML, I find that the count of the view to be correct but only 5 items are visible and when I scroll even those items get invisible. I did not face this issue when I use a ListModel in QML and have my data hard-coded as set of ListElements. Can someone explain my what's happening?
-
@VijayalakshmiSundaravaradhan
Since this never got answered in the past, if you do not get a reply here I suggest you join the PyQt mailing list (https://riverbankcomputing.com/mailman/listinfo/pyqt) and ask there. They'll tell you whether it's a PyQt issue or not.