Qt World Summit: Submit your Presentation

[SOLVED] What C++ equivalent has the QML point type?

  • Does anyone know what C++ equivalent the QML point type has?
    The Documentation says nothing about it. The Example shows only how it is created: Qt.point(10, 5) or as string "10, 5". That gives the assumption that its actually the QPoint class, or is it QPointF ?

    The reason i ask is that I want to make a QVector<QPointF> list accessible through QML. Has somebody done this already? Is there anything I should particularly pay attention to?

  • I would assume it's QPointF, which can be stored in a QVariant, so it shouldn't be difficult to expose it to QML.

  • Internally, it's a QPointF in storage, but a QQmlValueType (or QDeclarativeValueType, in 4.x) in terms of its JavaScript integration. Properties of the value type (eg, the x and y coordinates of a point) are exposed as QObject properties of the QQmlValueType specialisation, which has important implications. If you're only interested about the C++ type, though, you shouldn't need to worry (as accessing the property via the metaobject property system should return you a QVariant containing a QPointF).

  • Thanks for the replies and explanations. With the different types of lists in QML I'm always a little confused.

    If I have understood this right I need a QVariantList in my C++ class and expose it as property like this?:
    @class MyClass : public QObject {
    Q_PROPERTY(QVariantList pointData READ pointData WRITE setPointData)

    And how can I initialize the list in qml now? And is a manipulation with JavaScript possible?
    Can I do this?
    MyClass {
    pointData: [ "0,100.5", "1,101.5"]

    Component.onCompleted: pointData.append("2,102.5")

    Thanks again.

  • pointData: [ Qt.point(0, 100.5), Qt.point(1, 101.5) ] // should work
    The automatic assignment from strings (string conversion) codepaths are context-sensitive (that is, depending on what property type you're assigning to - and in this case, the property is not a QPointF and thus it won't convert correctly).

    I don't know about the .append() idea. pointData.push(Qt.point(2,102.5)) // might work in QtQuick2.

    The problem is that resolving "pointData" probably results in a temporary being created, to which you push/append -- but the temporary isn't then written back to the original property (most likely). You may have to do something like: Component.onCompleted: { var tmp = pointData; tmp.append(Qt.point(2,102.5)); pointData = tmp; } instead.

  • Thanks for that detailed explanation, it helped me a lot.
    I think the solution with Qt.point() initialization should suffice in my case.

    The more common use case is loading the points from a file via a "source" property, which I can do completly in C++.

Log in to reply