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. [SOLVED] how to assign a QList<listmodelObjects> to a listview in QML with QQmlListProperty?

[SOLVED] how to assign a QList<listmodelObjects> to a listview in QML with QQmlListProperty?

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 3 Posters 3.4k 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 last edited by
    #1

    if i have a class that contains a QList of listmodels (QList<listmodelobjects>), how would i assign a model from that list to a listview in QML?

    class code:
    @class TreeModel : public QAbstractItemModel
    {
    Q_OBJECT
    Q_PROPERTY(QQmlListProperty<ListModel> lists READ lists)

    public:

    enum AnimalRoles {
        TypeRole = Qt::UserRole + 1,
    };
    explicit TreeModel(const QString &data, QObject *parent = 0);
    ~TreeModel();
    
    QVariant data(const QModelIndex &index, int role) const Q_DECL_OVERRIDE;
    Qt::ItemFlags flags(const QModelIndex &index) const Q_DECL_OVERRIDE;
    QVariant headerData(int section, Qt::Orientation orientation,
                        int role = Qt::DisplayRole) const Q_DECL_OVERRIDE;
    QModelIndex index(int row, int column,
                      const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
    QModelIndex parent(const QModelIndex &index) const Q_DECL_OVERRIDE;
    int rowCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
    int columnCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
    
    QStringList getGroups();
    QStringList getFoldersByGroup(const QString &name);
    QStringList getFolderlistByGroupID(QStringList &name);
    void getFolders();
    void populateFrontPage();
    
    QQmlListProperty<ListModel> lists(){
            return QQmlListProperty<ListModel>(this, foldermodels);
    }
    
    
    ********************************************
    ListModel *groupmodel;
    QList<ListModel*> foldermodels;
    QList<ListModel*> filemodels;@
    

    now how would i assign for example, foldermodels.at(0) to a listview in qml?
    i have tried stuff like:
    @
    ListView {
    id: folderlist
    model: treemodel.lists // treemodel.lists.at(0) // treemodel.lists[0]
    delegate: folderDelegate
    contentHeight: contentItem.childrenRect.height
    height: childrenRect.height
    anchors.left: parent.left
    anchors.right: parent.right
    clip: true
    }@

    but i am getting errors like:

    @QMetaProperty::read: Unable to handle unregistered datatype 'QQmlListProperty<ListModel>' for property 'TreeModel::lists'
    QQmlExpression: Expression qrc:/main.qml:54:28 depends on non-NOTIFYable properties:
    TreeModel::lists
    QMetaProperty::read: Unable to handle unregistered datatype 'QQmlListProperty<ListModel>' for property 'TreeModel::lists'
    QQmlExpression: Expression qrc:/main.qml:54:28 depends on non-NOTIFYable properties:
    TreeModel::lists@

    and yes i have registered the Treemodel class containing the QList<modelObjects>.

    i also know the QList is actually populated with the correct models because the view shows the items when i do it like this in main.cpp

    @
    TreeModel model(infile.readAll());
    ListModel *foldermodel = model.foldermodels.at(1) // or (0)
    ctxt->setContextProperty("treemodel", &model);
    ctxt->setContextProperty("foldermodel", foldermodel);@

    Thanks in advance for the help, i really apreciate it!!

    1 Reply Last reply
    0
    • R Offline
      R Offline
      Rolias
      wrote on last edited by
      #2

      The error message indicates the error comes from line 54 which I don't see. Further it makes me think the code needs a notifiable type and you have the property set only for READ. I posted in your earlier question an offer for a VIP pass to my latest course which I think might help if you're new to integrating Qt Quick with C++.

      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
      • J Offline
        J Offline
        juddster
        wrote on last edited by
        #3

        I think the problem is that the class ListModel is not registered with the QML type system.

        You can register it using "qmlRegisterType":http://doc.qt.io/qt-5/qqmlengine.html#qmlRegisterType:
        @
        #include <QtQml>

        int main(int argc, char *argv[])
        {
        ...

        qmlRegisterType<ListModel>();
        
        ...
        

        }@

        Hope that helps.

        1 Reply Last reply
        0
        • D Offline
          D Offline
          Dragann
          wrote on last edited by
          #4

          juddster!! thanks! that did the trick.. now on to the next problem lol :P

          thanks again!

          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