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 access QList<QObject*> items in QML
Qt 6.11 is out! See what's new in the release blog

How to access QList<QObject*> items in QML

Scheduled Pinned Locked Moved QML and Qt Quick
9 Posts 6 Posters 12.1k 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.
  • B Offline
    B Offline
    borut123
    wrote on last edited by
    #1

    I pass QList<QObject*> from Qt to QML:
    context->setContextProperty("listViewModel", QVariant::fromValue(data));

    And then use it as Listview's model to fill the ListView.
    All that works correctly.

    I also want to access model's items from other places, not just from ListView
    It should be possible to do it like that (for first item): listViewModel.get(0);

    But I get the error:
    TypeError: Result of expression 'listViewModel.get' [undefined] is not a function.

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

      as variant, you can write your own qml element in C++ which will be wrapper on the listViewModel

      1 Reply Last reply
      0
      • A Offline
        A Offline
        alexander
        wrote on last edited by
        #3

        @class CustomObject : public QObject
        {
        ...
        Q_INVOKABLE void setModel( QAbstractItemModel* ) {
        QAbstractItemModel* _model

        }
        Q_INVOKABLE QVariant item(int index) {
        return _model->data( _model->index(index) );
        }
        ...
        }@

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mbrasser
          wrote on last edited by
          #4

          Hi,

          The initial release of QML for the most part focused on list/model support as a source of data for views. You can track issue "QTBUG-14986":http://bugreports.qt.nokia.com/browse/QTBUG-14986 for full list manipulation support from within QML.

          Regards,
          Michael

          1 Reply Last reply
          0
          • Q Offline
            Q Offline
            qtrahul
            wrote on last edited by
            #5

            You can see the how to access the list model element in QML.

            You should check the below link.

            http://doc.qt.nokia.com/4.7-snapshot/qml-listmodel.html

            Best regards,

            1 Reply Last reply
            0
            • K Offline
              K Offline
              kamalakshantv
              wrote on last edited by
              #6

              [quote author="qtrahul" date="1292918337"]You can see the how to access the list model element in QML.

              You should check the below link.

              http://doc.qt.nokia.com/4.7-snapshot/qml-listmodel.html

              Best regards,[/quote]

              Rahul,

              Please read the query and reply accordingly.

              1 Reply Last reply
              0
              • R Offline
                R Offline
                raja26
                wrote on last edited by
                #7

                Have you found any way to do this? Without writing doing complex QAbstractListModel?

                1 Reply Last reply
                0
                • B Offline
                  B Offline
                  borut123
                  wrote on last edited by
                  #8

                  No other way. Try QAbstractListModel, it's not so difficult as it sounds.

                  1 Reply Last reply
                  0
                  • R Offline
                    R Offline
                    raja26
                    wrote on last edited by
                    #9

                    [quote author="borut123" date="1345498136"]
                    No other way. Try QAbstractListModel, it's not so difficult as it sounds.[/quote]

                    Its not difficult. But for doing simple things like this why should we sub class one more class and deal with defining properties, functions etc, etc. This is how I feel. I'm alread having a tough time dealing with poniters (Esp. deleting QThread objects). So I don't want to create one more class. :-(

                    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