Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. ListModel defined in C++, trying to print count in QML

ListModel defined in C++, trying to print count in QML

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 1.2k 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.
  • P Offline
    P Offline
    poncho524
    wrote on last edited by
    #1

    I have a ListModel in C++ defined as

    QList<QObject*> myModel
    [...]
    setContextProperty("myModel", QVariant::fromValue(myModel));
    

    On the QML side i want to see how many items are in the list... but when i try something like

    console.log("list size: " + myModel.count)
    

    all i see print out is

    list size: undefined
    

    Help??

    E 1 Reply Last reply
    0
    • P poncho524

      I have a ListModel in C++ defined as

      QList<QObject*> myModel
      [...]
      setContextProperty("myModel", QVariant::fromValue(myModel));
      

      On the QML side i want to see how many items are in the list... but when i try something like

      console.log("list size: " + myModel.count)
      

      all i see print out is

      list size: undefined
      

      Help??

      E Offline
      E Offline
      Eeli K
      wrote on last edited by
      #2

      @poncho524 Did you try myModel.count()? It's a function, not a property.

      1 Reply Last reply
      0
      • P Offline
        P Offline
        poncho524
        wrote on last edited by
        #3

        According to this (http://doc.qt.io/qt-5/qml-qtqml-models-listmodel.html) "count" is a property.

        However, I did try count() and it gave a bigger error.

        E 1 Reply Last reply
        0
        • P poncho524

          According to this (http://doc.qt.io/qt-5/qml-qtqml-models-listmodel.html) "count" is a property.

          However, I did try count() and it gave a bigger error.

          E Offline
          E Offline
          Eeli K
          wrote on last edited by
          #4

          @poncho524 But the documentation you gave is for the QML ListModel. It's different than a C++ context property model. Maybe you should inspect the QML tree at runtime with the QML debugger and check out what your model object really is. You may be able to handle it as a javascript object.

          Actually QList doesn't have properties or slots and it's not even a QObject so you can't use its functions in QML. It's possible that you have to implement a QAbstractItemModel C++ model if you want to handle the model in both C++ and QML.

          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