Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. PyQt5: Invisible items in a QML ListView (perhaps a bug?)

PyQt5: Invisible items in a QML ListView (perhaps a bug?)

Scheduled Pinned Locked Moved Language Bindings
6 Posts 4 Posters 3.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MazeChaZer
    wrote on last edited by
    #1

    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.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.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

    Edit:

    here is a little screencast where you can see the problem: http://gfycat.com/OffensiveShinyHagfish

    1 Reply Last reply
    0
    • M Offline
      M Offline
      MazeChaZer
      wrote on last edited by
      #2

      Can anybody reproduce this problem with their own setup?

      1 Reply Last reply
      0
      • H Offline
        H Offline
        Hazzl
        wrote on last edited by
        #3

        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_())
        
        1 Reply Last reply
        0
        • V Offline
          V Offline
          VijayalakshmiSundaravaradhan
          wrote on last edited by
          #4

          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?

          JonBJ 1 Reply Last reply
          0
          • V VijayalakshmiSundaravaradhan

            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?

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @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.

            V 1 Reply Last reply
            0
            • JonBJ JonB

              @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.

              V Offline
              V Offline
              VijayalakshmiSundaravaradhan
              wrote on last edited by
              #6

              @JonB Thanks

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved