ListModel only showing first item when displayed using Qt.createQmlObject



  • I am trying to dynamically create a ListModel and place it within a Rectangle. The problem is that only the first item of the List is showing up and the rest just aren't there. If I copy and paste the generated code in directly and pretend it wasn't dynamically generated but remove the imports (and the Item.. same problem if I leave the item in there!), then it works as desired. However, the generated code (as shown below after generation) requires those including the Item in order for Qt.createQmlObject to not throw an error.

    Maybe I can find some other way to remove the Item which seems to be causing problems? I tried using anchors.fill in order to make sure that it isn't an issue with the size being wrong.

    Please note that this generated code is being inserted within:

    ApplicationWindow {
       ColumnLayout {
         Loader {
            Item { 
              Rectangle {   //This is the parent passed into Qt.createQmlObject
    

    This has been bugging me for a couple days, your help/time is much appreciated!

    var buttonQMLString =
                "import QtQuick 2.0
                 import QtQuick.Controls 1.5
                 import QtQuick.Layouts 1.3
    
                  Item {
                   ListModel {
                     id: buttonListModel
                     ListElement {buttonText: \"Button1\"}
                     ListElement {buttonText: \"Button2\"}
                     ListElement {buttonText: \"Button3\"}
                     ListElement {buttonText: \"Button4\"}
                     ListElement {buttonText: \"Button5\"}
                 }
    
                 Component {
                            id: buttonListDelegate
                            Button {
                              text: buttonText
                            }
                          }
    
                          ListView {
                            anchors.fill: parent
                            model: buttonListModel
                            delegate: buttonListDelegate
                          }
                          }"
    


  • I'm convinced this is a QML bug and I'm going to have to write my own QAbstractItemModel:
    http://doc.qt.io/qt-5/qtquick-modelviewsdata-cppmodels.html

    But I'd prefer not to.. anyone have any advice?


  • Moderators

    Hi @mczarnek
    I think you should add anchors.fill: parent for the root Item which contains ListModel and ListView. So,

    import QtQuick.Controls 1.5
    import QtQuick.Layouts 1.3
    
    Item {
        anchors.fill: parent /* Note this part */
        ListModel {
        id: buttonListModel
        ...
    

    This is because this one is the actual item which is going to be a child of the item which you pass to createQmlObject.



  • I could have sworn I tried that, however I certainly will try it again when I get access to my computer again in a couple days. Perhaps it was on a different component. Will post back at that point.

    Anyone have other thoughts?



  • Tried it didn't work, thanks for the suggestion though!


  • Moderators

    @mczarnek Well then can you post a minimal complete example which can reproduce the same behavior ? It will be helpful to find the exact cause.



  • Sure, I'm new to Qt and this is one of the first features I'm trying to create, so might as well just upload the project:
    http://speedy.sh/2GM4C/FractalClient.zip

    There is not much else going on. I'm using the 'userdatastore' object to dynamically load the names from a file, then create the ListModel based on that.

    Look in 'UserChooser.qml', the important piece is mostly in "Component.onCompleted". The commented out code is attempting to create the ListModel, then dynamically add to it. Didn't work any better than the other version.

    Edit: I believe the issues is in the anchors for the bottom. Started playing with them and got them to show all buttoms... just in the wrong place.

    Thank you!


Log in to reply
 

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