How to get lists dynamically from c++



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



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



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



  • 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
    }@



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



  • Thanks Rolias!! i have sent you an email already



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



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



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



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


Log in to reply
 

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