Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. ListModel only showing first item when displayed using Qt.createQmlObject
Forum Update on Monday, May 27th 2025

ListModel only showing first item when displayed using Qt.createQmlObject

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 2 Posters 2.0k 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.
  • mczarnekM Offline
    mczarnekM Offline
    mczarnek
    wrote on last edited by mczarnek
    #1

    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
                          }
                          }"
    
    mczarnekM 1 Reply Last reply
    0
    • mczarnekM mczarnek

      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
                            }
                            }"
      
      mczarnekM Offline
      mczarnekM Offline
      mczarnek
      wrote on last edited by
      #2

      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?

      p3c0P 1 Reply Last reply
      0
      • mczarnekM mczarnek

        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?

        p3c0P Offline
        p3c0P Offline
        p3c0
        Moderators
        wrote on last edited by p3c0
        #3

        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.

        157

        1 Reply Last reply
        1
        • mczarnekM Offline
          mczarnekM Offline
          mczarnek
          wrote on last edited by
          #4

          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?

          1 Reply Last reply
          0
          • mczarnekM Offline
            mczarnekM Offline
            mczarnek
            wrote on last edited by
            #5

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

            p3c0P 1 Reply Last reply
            0
            • mczarnekM mczarnek

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

              p3c0P Offline
              p3c0P Offline
              p3c0
              Moderators
              wrote on last edited by
              #6

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

              157

              1 Reply Last reply
              0
              • mczarnekM Offline
                mczarnekM Offline
                mczarnek
                wrote on last edited by mczarnek
                #7

                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!

                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