Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Exposing list of Qpoint from C++ to Qml
Qt 6.12 LTS is out! See what's new in the release blog

Exposing list of Qpoint from C++ to Qml

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

    Hi,

    I'm trying to expose a list of Qpoint from C++ to qml.
    I've added this @ Q_PROPERTY(QVariantList tab READ tab)@ and this @
    QVariantList TestList::tab() const
    {
    QVariantList list;
    list.append(QPoint(5,12));
    list.append(QPoint(6,0));
    list.append(QPoint(45,2));
    list.append(QPoint(9,17));
    list.append(QPoint(3,15));

    return list;
    

    }@

    And in Qml side I'm trying that : @import QtQuick 1.0

    Rectangle {
    width: 100
    height: 100
    color: "red"

    TestList{
        id:list
    }
    
    Component.onCompleted:{
        console.log("leght:"+list.tab.length);
        for (var i=0; i<list.tab.length; i++)
        {
            console.log("Array item:", list.tab[i]," "+list.tab[i].x)
        }
    }
    

    }@

    But I can't access my QPoint data this way it said "undefined" for the ".x" part. And I don't know how to do it. Did I miss something ?

    Thanks for reading me.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      minimoog77
      wrote on last edited by
      #2

      See http://doc.qt.nokia.com/4.7/declarative-tutorials-extending-chapter5-listproperties.html

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        I want a read only property and I'm not sure it's possible with this solution. Can I just don't set any append function?

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          A model can be defined by subclassing QAbstractItemModel. This is the best approach if you have a more complex model that cannot be supported by the other approaches. A QAbstractItemModel can also automatically notify a QML view when the model data has changed.

          http://developer.qt.nokia.com/doc/qt-4.7/qdeclarativemodels.html#id-3ebf8f48-0c48-46b0-9937-38b514d927c1

          1 Reply Last reply
          0
          • S Offline
            S Offline
            srikanth_trulyit
            wrote on last edited by
            #5

            Problem is QML is not understanding how to access QPoint::x() method. It is neither a slot nor declared as Q_INVOKABLE.

            In order to do that create a QObject wrapper around QPoint and supply Q_INVOKABLE methods namely x(), y() which inturn returns the underlying QPoint x,y. Expose this as a QObjectList to qml. Things should work.

            Also for your reference see "qml basic type point":http://doc.qt.nokia.com/latest/qml-point.html and
            Qt.point() so that you can rethink your implementation.

            1 Reply Last reply
            0
            • ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #6

              That's seems to be a good solution, I will look in this way.
              I've read this reference, but I don't see how thats could help me.

              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
              • Unsolved