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. What is the simplest way of exposing a vector of custom c++ objects with Q_Properties to qml?
Forum Updated to NodeBB v4.3 + New Features

What is the simplest way of exposing a vector of custom c++ objects with Q_Properties to qml?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
4 Posts 3 Posters 1.2k Views 2 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.
  • M Offline
    M Offline
    maxwell31
    wrote on last edited by
    #1

    Hi,

    I have a class like this

    class MyClass{
    Q_GADGET
    Q_PROPERTY(QString programName READ programName)
    Q_PROPERTY(QDateTime timeStamp MEMBER _timestamp)
    Q_PROPERTY(QUrl iconFile MEMBER _icon)
    

    I did notmake it a QObject, as otherwise I get errors regarding call to implicitly-deleted copy constructor, as I use the implicit copy constructor of my object at other places.

    Then I have a std::vector<MyClass> of these objects. What is the simplest way of exposing my vector to QML so I can use it as a model with my properties as roles?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      What about Extending QML - Object and List Property Types Example chapter of Qt's documentation ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • M Offline
        M Offline
        maxwell31
        wrote on last edited by
        #3

        Yes this works. I just thought if there might not be an easier way. One of the things I don't like is that I need to derive from QObject (does not work with copy constructors).

        J.HilkJ 1 Reply Last reply
        0
        • M maxwell31

          Yes this works. I just thought if there might not be an easier way. One of the things I don't like is that I need to derive from QObject (does not work with copy constructors).

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by J.Hilk
          #4

          @maxwell31

          have you tried to store your Class objects in a QList<myClass> *myList?

          And than access it from qml via

          QVariant getClassList(){QVariant::fromValue(myList)}
          

          works fine with QObjects, should work with QGadget, but, I'm not 100% sure.


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          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