April 11, 2012

z.emb z.emb
Lab Rat
24 posts

[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?

5 replies

April 11, 2012

mlong mlong
Robot Herder
1551 posts

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

 Signature 

Senior Software Engineer
AccuWeather Enterprise Solutions
/* My views and opinions do not necessarily reflect those of my employer.  Void where prohibited. */

April 12, 2012

chriadam chriadam
Lab Rat
181 posts

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).

April 12, 2012

z.emb z.emb
Lab Rat
24 posts

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?:

  1. class MyClass : public QObject {
  2.   Q_OBJECT
  3.   Q_PROPERTY(QVariantList pointData READ pointData WRITE setPointData)
  4. ...
  5. }

And how can I initialize the list in qml now? And is a manipulation with JavaScript possible?
Can I do this?

  1. MyClass {
  2.  pointData: [ "0,100.5", "1,101.5"]
  3.  
  4.  Component.onCompleted: pointData.append("2,102.5")
  5. }

Thanks again.

April 12, 2012

chriadam chriadam
Lab Rat
181 posts

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.

April 12, 2012

z.emb z.emb
Lab Rat
24 posts

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++.

 
  ‹‹ Calling QCamera::SetViewfinder in examples/declarative-camera      [solved] Getting the last known position from GPS ››

You must log in to post a reply. Not a member yet? Register here!