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. Populate QML with nested lists from Qt?

Populate QML with nested lists from Qt?

Scheduled Pinned Locked Moved QML and Qt Quick
2 Posts 1 Posters 2.2k Views 1 Watching
  • 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.
  • A Offline
    A Offline
    AmanVirdi
    wrote on last edited by
    #1

    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_OBJECT
    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)
    public:
    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);
    

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

    private:
    qint32 m_trackid;
    qint32 m_trackno;
    qint32 m_duration;
    QString m_tracktitle;
    };@

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

    public:
    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);
    

    signals:
    void albumidChanged();

    private:
    qint32 m_albumid;
    QList<TrackList*> m_tracklist;
    };
    @

    main.cpp:

    @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
                            smooth:true
                            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.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      AmanVirdi
      wrote on last edited by
      #2

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

      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