Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to get lists dynamically from c++
Forum Update on Monday, May 27th 2025

How to get lists dynamically from c++

Scheduled Pinned Locked Moved QML and Qt Quick
10 Posts 3 Posters 2.1k 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.
  • D Offline
    D Offline
    Dragann
    wrote on 22 Feb 2015, 21:49 last edited by
    #1

    Hello folk,

    i have a set of listmodels in c++ that i am trying to view in QML.

    suppose i have 2 groups and each group contains a certain number of folders.

    i have written a few function that create seperate lists for each level of items. So now i have 1 list that contains all the groups and 2 lists that contain the folders of the groups

    list 1 = groups (2 groups in this case)
    list 2 = folders of group 1
    list 3 = folders of group 2

    @Group1 (list1)
    Folder1 (list2)
    Folder2 (list2)
    Group2 (list1)
    Folder1 (list2) <-- (in this case both folder lists contain the same items)
    Folder2 (list2)@

    The following code creates a nested list of list 1 and list 2

    @Rectangle {
    height: 300
    width: 300

    Component {
        id: folderDelegate
        Item {
            width: 100
            height: col2.childrenRect.height
    
            Column {
                id: col2
                anchors.left: parent.left
                anchors.right: parent.right
    
                Text {
                    id: name1
                    text: " " + model.Name
                }
            }
        }
    }
    
    ListView {
        id: outer
        model: myModel
        delegate: groupsDelegate
        anchors.fill: parent
    }
    
    Component {
        id: groupsDelegate
    
        Item {
            width: 100
            height: col.childrenRect.height
    
            Column {
                id: col
                anchors.left: parent.left
                anchors.right: parent.right
    
                Text {
                    id: t1
                    text: model.Name
                }
    
                ListView {
                    id: folderlist
                    model: foldermodel
                    delegate: folderDelegate
                    contentHeight: contentItem.childrenRect.height
                    height: childrenRect.height
                    anchors.left: parent.left
                    anchors.right: parent.right
                    clip: true
                }
            }
        }
    }
    

    }@

    what i am trying to do is to add list 3 beneath the 2nd item of the group list

    so this:
    @Group1 (list1)
    Folder1 (list2)
    Folder2 (list2)
    Group2 (list1)
    Folder1 (list3) <-- (the folders of group2 should be displayed here)
    Folder2 (list3)@

    so my question is, how can i add the list dynamically?
    i was thinking of writing a function in c++ that gives QML the name of the listmodel through a QString. But doing something like

    @ListView {
    model: object.getStr() <<---(function that returns the name of a model with QString
    } @

    function:
    @Q_INVOKABLE QString getStr( return name );@

    but this does not work.

    how could i get something like that to work?

    after this, i would also need to be able to click on a folder to make a new screen appear that holds a list of all the items in the folder. I think that if i can make this work, expanding it to different levels of detail should work aswell

    Thanks in advance!!

    1 Reply Last reply
    0
    • G Offline
      G Offline
      Gianluca
      wrote on 23 Feb 2015, 07:41 last edited by
      #2

      You have to use QQmlListProperty in C++.
      This template is exactly for your problem, allow you to wrap a QList of QObject or your custom list class, and to pass to QML side in such a way it can be used as delegate into ListView (& Co.).

      1 Reply Last reply
      0
      • D Offline
        D Offline
        Dragann
        wrote on 23 Feb 2015, 10:08 last edited by
        #3

        that sounds good, i will try it out! thanks!!

        1 Reply Last reply
        0
        • D Offline
          D Offline
          Dragann
          wrote on 23 Feb 2015, 16:25 last edited by
          #4

          allright, so far so good... i have made all the lists and added the qqmllistproperty... Now my question is, how do i pass it to qml?

          @ListView {
          id: folderlist
          model: treemodel.lists[0] <<===== (does not work)
          delegate: folderDelegate
          contentHeight: contentItem.childrenRect.height
          height: childrenRect.height
          anchors.left: parent.left
          anchors.right: parent.right
          clip: true
          }@

          1 Reply Last reply
          0
          • R Offline
            R Offline
            Rolias
            wrote on 24 Feb 2015, 19:40 last edited by
            #5

            If you want some background knowledge on working with QML from C++ I just published a course on that this month: Integrating Qt Quick with C++. http://bit.ly/qtquickcpp. This is the third course in the series. Watching it might save you some time.

            Pluralsight is a subscription site but they have a free ten-day trial (limited to 4 hours) and I also have some VIP passes left. The VIP pass is good for a week but includes the ability to watch unlimited hours during the week, download the course materials, and watch offline (offline content will expire with the pass). If you want a pass, just send me an email through this forum.

            Check out my third course in the trilogy on Qt
            "Integrating Qt Quick with C++"
            http://bit.ly/qtquickcpp
            published by Pluralsight

            1 Reply Last reply
            0
            • D Offline
              D Offline
              Dragann
              wrote on 25 Feb 2015, 12:12 last edited by
              #6

              Thanks Rolias!! i have sent you an email already

              1 Reply Last reply
              0
              • R Offline
                R Offline
                Rolias
                wrote on 25 Feb 2015, 19:13 last edited by
                #7

                The pass info has been sent. You might need to check you junk mail if no-reply@qt-project .org isn't on your white list.

                Check out my third course in the trilogy on Qt
                "Integrating Qt Quick with C++"
                http://bit.ly/qtquickcpp
                published by Pluralsight

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  Dragann
                  wrote on 25 Feb 2015, 19:55 last edited by
                  #8

                  ah yes! very good. I will register tomorrow and start following the lessons. If its good i will maybe even pay subscription. How many hours on material would there be on the website?

                  1 Reply Last reply
                  0
                  • R Offline
                    R Offline
                    Rolias
                    wrote on 26 Feb 2015, 19:22 last edited by
                    #9

                    My three courses require about 12 hours to watch. There is also a course, Python Desktop Application Development, by Bo Milanovich that uses Qt (I haven't watched it yet) that takes another 4. There are also many courses on C++ and software development in general.

                    If you meant the entire site then I suspect there are tens of thousands of hours, (I'll probably get an up-to-date number at this weekend's annual author summit).

                    Plus I think a subscription to Pluralsight also gets you access to all the content on Digital Tutors. The PeepCode and TrainSignal content has been integrated into the main library. Pluralsight recently acquired CodeSchool but that content is separate for now.

                    Check out my third course in the trilogy on Qt
                    "Integrating Qt Quick with C++"
                    http://bit.ly/qtquickcpp
                    published by Pluralsight

                    1 Reply Last reply
                    0
                    • D Offline
                      D Offline
                      Dragann
                      wrote on 26 Feb 2015, 19:25 last edited by
                      #10

                      i am using the VIP pass allready so i can see how much content there is already :) Thanks alot for the VIP pass, i am using it now to watch the c++ and the qt qml + qtquick courses.

                      and yes, it seems the subscription gives access to everything because i can access all the courses so its very nice!

                      ill probably extend my subscription after this week!

                      1 Reply Last reply
                      0

                      1/10

                      22 Feb 2015, 21:49

                      • Login

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