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. Using custom C++ models in QML

Using custom C++ models in QML

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 3 Posters 3.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.
  • A Offline
    A Offline
    astorije
    wrote on last edited by
    #1

    Hello,

    We use QML to draw the 2D user interface of a 3D software.

    We are building models in C++, that we send to QML, to use in ListViews for example.
    The models inherit from QAbstractListModel.

    The problem is we cannot use the common accessors from the ListModel encapsulated in the ListView, because there is no interface provinding those method declarations in C++.

    What is the common and proper way to define our own models in C++ so that we can use them in QML for both reading and writing?

    Thanks !

    1 Reply Last reply
    0
    • T Offline
      T Offline
      task_struct
      wrote on last edited by
      #2

      Hello,

      The only thing I found in documentation is "this":http://doc.qt.nokia.com/4.7-snapshot/qdeclarativemodels.html#accessing-views-and-models-from-delegates . Look at the end of page.

      "Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program."

      • Linu...
      1 Reply Last reply
      0
      • M Offline
        M Offline
        mbrasser
        wrote on last edited by
        #3

        Hi,

        In general you will need to add your own slots (or Q_INVOKABLE functions) for any functionality not provided out of the box by QAbstractListModel. You could, for example, add a setter like ListModel

        @ Q_INVOKABLE void setProperty(int index, const QString& property, const QVariant& value);@

        and implement the appropriate logic for your model.

        Regards,
        Michael

        1 Reply Last reply
        0
        • A Offline
          A Offline
          astorije
          wrote on last edited by
          #4

          Hi,

          Thanks for the answers guys, it confirmed our thoughts!

          Finally, what we will do is to implement the ListModel methods within our custom models, so that we will be able to use both the standard ListModels and our custom models in our custom QML components, or the standard ListView and other StuffView components.

          We think it is the more sustainable and generic solution within our range of possibilities...
          Any counter-arguments? :-)

          Regards,

          Jérémie

          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