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 QQmlApplicationEngine

    def main():
    app = QApplication(sys.argv)

    engine = QQmlApplicationEngine()
    engine.rootObjects()[0].setProperty("model", TestModel())

    class TestModel(QAbstractListModel):
    def init(self):
    super().init() = ["a", "b", "c"]

    def rowCount(self, QModelIndex_parent=None, *args, **kwargs):
        return len(
    def data(self, QModelIndex, int_role=None):
    def roleNames(self):
        return {Qt.UserRole + 1: "test"}

    if name == "main":

    QML file
    import QtQuick 2.0
    import QtQuick.Controls 1.2

    ApplicationWindow {
    id: app
    visible: true
    width: 400
    height: 700
    property alias model: list.model

    ListView {
        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.2


    here is a little screencast where you can see the problem:

  • 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)

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.