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. QVector<CustomClass> as a var property in QML. Qt 5.14 vs 5.12.
Forum Updated to NodeBB v4.3 + New Features

QVector<CustomClass> as a var property in QML. Qt 5.14 vs 5.12.

Scheduled Pinned Locked Moved Solved QML and Qt Quick
6 Posts 2 Posters 2.1k 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.
  • D Offline
    D Offline
    Dominik1983
    wrote on last edited by Dominik1983
    #1

    Hello Ladies and Gentlemen.

    Unsure if my idea would work, I created a DataPoint class marked with Q_GADGET with three properties defined using Q_PROPERTY. I placed Q_DECLARE_METATYPE(DataPoint) after the class definition. I also added qRegisterMetaType<DataPoint>() to the initialisation method of my main C++ class (let's call it MainClass).

    In my MainClass I defined a property:

    Q_PROPERTY(QVector<DataPoint> highPressureVector READ getHighPressureVector NOTIFY highPressureVectorChanged)
    

    I fill a corresponding MainClass member (that is used in the getter) m_highPressureVector with three DataPoint objects.

    In QML I use the forementioned property like this:

    property var points: mainObject ? mainObject.highPressureVector : []
    

    When I log its contents with:

    console.log("points = " + points + ", points.length = " + points.length)
    

    I get what I expect:

    qml: points = DataPoint(0, 1, red),DataPoint(1,2,red),DataPoint(2,3,red), points.length = 3
    

    The PROBLEM is that I did all this in Qt 5.14.2. I learned that we will use Qt 5.12.8. After compiling the exact same code with Qt 5.12.8 the line that prints points property looks like this:

    qml: points = QVariant(QVector<DataPoint>), points.length = undefined
    

    QUESTION: Is there a way to keep using QVector<DataPoint> as my property? Or do I have to make DataPoint inherit from QObject and use QVariantList or something like that?

    I also have a second less important question. Why does this happen? I haven't seen anything in the Qt 5.14 version description that would justify such a difference in behaviour.

    1 Reply Last reply
    0
    • D Offline
      D Offline
      Dominik1983
      wrote on last edited by
      #2

      It seems that I'm not the only one who has encountered this problem:
      https://stackoverflow.com/questions/58284209/how-to-pass-qvectorcustom-q-gadget-to-qml

      There is also a bug for that but it's still open even though QVector<GadgetClass> works in Qt 5.14.
      https://bugreports.qt.io/browse/QTBUG-73399

      It seems that someone changed something in 5.14 that is not well documented but has made a significant change in handling QVectors of gadgets.

      Since (for Qt 5.12) QML displays my points property as

      QVariant(QVector<DataPoint>)
      

      I assume that it is aware of the existence of DataPoint type. Why does it wrap it in QVariant in Qt 5.12? Is there a way to extract my vector from this variant so that QML treats it as an array?

      1 Reply Last reply
      0
      • D Offline
        D Offline
        Dominik1983
        wrote on last edited by
        #3

        OK, I gave up.

        Instead of using Q_GADGET I made DataPoint class inherit from QObject and added Q_OBJECT macro to it. Then I turned my property which stores DataPoints into a QList:

        Q_PROPERTY(QList<QObject*> highPressureList READ getHighPressureList NOTIFY highPressureListChanged)
        

        I wish I could have kept QVector<GadgetClass> though...

        I think that this is the change that is missing in 5.12 and is present in 5.14:
        https://bugreports.qt.io/browse/QTBUG-60338

        1 Reply Last reply
        0
        • fcarneyF Offline
          fcarneyF Offline
          fcarney
          wrote on last edited by
          #4

          Does it make any different to register QVector<DataPoint> as well:

          qRegisterMetaType<QVector<DataPoint>>()
          

          C++ is a perfectly valid school of magic.

          D 1 Reply Last reply
          0
          • fcarneyF fcarney

            Does it make any different to register QVector<DataPoint> as well:

            qRegisterMetaType<QVector<DataPoint>>()
            
            D Offline
            D Offline
            Dominik1983
            wrote on last edited by
            #5

            I had some time to return to this problem and switch back DataPoint to Q_GADGET.

            @fcarney said in QVector<CustomClass> as a var property in QML. Qt 5.14 vs 5.12.:

            Does it make any different to register QVector<DataPoint> as well:

            qRegisterMetaType<QVector<DataPoint>>()
            

            Unfortunately it did not help. The vector in QML was visible as:

            QVariant(QVector<DataPoint>)
            
            1 Reply Last reply
            0
            • D Offline
              D Offline
              Dominik1983
              wrote on last edited by
              #6

              I found a satisfying solution. When my DataPoint class is a Q_GADGET and is registered as a metatype:

              Q_DECLARE_METATYPE(DataPoint);
              
              qRegisterMetaType<DataPoint>();
              

              it can be used in a QVariantList. The list is visible in QML as a JavaScript array. Then I can freely access all properties of each DataPoint in QML.

              Adding an object of my DataPoint class to a QVariantList exposed to QML looks like this:

              list.append(QVariant::fromValue(DataPoint{x, y, color}));
              

              Reading a single DataPoint object from the QVariantList looks for example like this:

              DataPoint lastdp = list.last().value<DataPoint>();
              
              1 Reply Last reply
              1

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved