Populate QML with nested lists from Qt?

  • I want to display structure like:

    Album A
    Track A1
    Track A2
    Album B
    Track B1
    Track B2

    I have made two classes TrackList and TrackDataModel. The TrackDataModel is having QList of TrackList type.
    I am not able to access the Child list i.e. tracklist in QML file. There is no error but tracklist details are not displaying on QML file.
    I have written something like follows, am I following right approach?

    TrackList.h file looks like:
    class TrackList: public QObject
    Q_PROPERTY(qint32 trackid READ trackid WRITE setTrackId NOTIFY trackidChanged)
    Q_PROPERTY(qint32 trackno READ trackno WRITE setTrackNo NOTIFY tracknoChanged)
    Q_PROPERTY(qint32 duration READ duration WRITE setDuration NOTIFY durationChanged)
    Q_PROPERTY(QString tracktitle READ tracktitle WRITE setTrackTitle NOTIFY tracktitleChanged)
    TrackList(QObject *parent=0);
    TrackList(const qint32 &trackid, const qint32 &trackno, const qint32 &duration, const QString &tracktitle, QObject *parent=0);

    qint32 trackid() const;
    void setTrackId(const qint32 &trackid);
    qint32 trackno() const;
    void setTrackNo(const qint32 &trackno);
    qint32 duration() const;
    void setDuration(const qint32 &duration);
    QString tracktitle() const;
    void setTrackTitle(const QString &tracktitle);

    void trackidChanged();
    void tracknoChanged();
    void durationChanged();
    void tracktitleChanged();

    qint32 m_trackid;
    qint32 m_trackno;
    qint32 m_duration;
    QString m_tracktitle;

    TrackDataModel.h file looks like:
    @class TrackDataModel: public QObject
    Q_PROPERTY(qint32 albumid READ albumid WRITE setAlbumId NOTIFY albumidChanged)
    Q_PROPERTY(QList<TrackList*> tracklist READ tracklist WRITE setTrackList)

    TrackDataModel(QObject *parent=0);
    TrackDataModel(const qint32 &albumid, const QList<TrackList *> &tracklist, QObject *parent);

    qint32 albumid() const;
    void setAlbumId(const qint32 &albumid);
    QList<TrackList*> tracklist() const;
    void setTrackList(const QList<TrackList*> &tracklist);

    void albumidChanged();

    qint32 m_albumid;
    QList<TrackList*> m_tracklist;


    @QMap<int, QObject*> TrackMap;

    TrackMap = //data filled by calling function here
    viewer->rootContext()->setContextProperty("trackModel", QVariant::fromValue(TrackMap.values()));@

    and .qml file is:
    Rectangle {
    Component {
    id: trackDelegate

        Item {
            id: parentItem
            width: grid.cellWidth
            height: grid.cellHeight
            Item {
                id: albumsDetail
                width: 200
                anchors.left: parent.left
                Text {
                    id: txtAlbumId
                    text: albumid                                 //TrackDataModel::albumid is being show here correctley
            Item {
                id: tacksDetail
                width: 300
                height: 200
                anchors.left: albumsDetail.right
                anchors.leftMargin: 100
                ListView {
                    id: lstTracksDetail
                    model: tracklist                           // What should be the model here
                    anchors.fill: parent
                    delegate: Row {
                        Text {
                            id: txtTrackNo
                            text: trackno                      // how can I access the TrackList elements here
                            width: lstTracksDetail.width/3
                            height: 20
                            font.family: fontFamilyNormal
                            clip: true
                        Text {
                            id: txtTitle
                            text: "tracktitle"                   // how can I access the TrackList elements here
                            width: 100
                            height: 20
                        Text {
                            id: txtDuration
                            text: "duration"
                            width: 100
                            height: 20
    GridView {
        id: grid
        cellWidth: 300
        cellHeight: 200
        model: trackModel                                //TrackDataModel is available here
        snapMode: GridView.SnapToRow
        delegate: trackDelegate
        focus: true


    in qml there is no error while running but the track's info is blank. I am not able to access the TrackList in the inner delegate.
    Please help me to overcome this issue.

  • No one has solution for my problem? Please help me...

Log in to reply

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