[Interest] Creating Q_GADGET from qml

Dmitry Volosnykh dmitry.volosnykh at gmail.com
Fri Nov 25 09:57:19 CET 2016


Hello, Immanuel!

I think you have missed this documentation page:
http://doc.qt.io/qt-5/positioning-cpp-qml.html

Quote:
Using QGeoCoordinate as an example, the C++ types are directly exposed to
the QML environment via its meta type:
qRegisterMetaType<QGeoCoordinate>();
QMetaType::registerEqualsComparator<QGeoCoordinate>();

Regards,
Dmitry.

On Fri, Nov 25, 2016 at 11:38 AM Immanuel Weber <immanuel.weber at gmail.com>
wrote:

Hi all,
I'm trying to expose a class DoubleVector2D which is a Q_GADGET to qml.
That class looks like this:
class DoubleVector2D
{
Q_GADGET
Q_PROPERTY(double x READ x WRITE setX)
Q_PROPERTY(double y READ y WRITE setY)
public:
DoubleVector2D() {}
DoubleVector2D(double x, double y) : m_x(x), m_y(y) {}
double x() const{ return m_x; }
double y() const { return m_y; }
void setX(double x) { m_x = x; }
void setY(double y) { m_y = y; }
private:
double m_x = 0.0;
double m_y = 0.0;
};
In my main.cpp I register it using
qRegisterMetaType<DoubleVector2D>("DoubleVector2D");
In a seperate class which is QObject derived I have a function which
returns such a DoubleVector2D.
Calling that function in my qml file returns the DoubleVector2D, so the
class works with qml in principal.
However I also need/want to create objects of that class in my qml/js code.
I saw that QtPositioning is using Q_GADGET in the same way for example to
expose QGeoCoordinate to qml.
There I can write QtPositioning.coordinate() and create an object of that
class. I examined the source of QtPositioning, however I could not find out
how this is done. qmlRegisterType() obiously does not work, as it is used
to register QObject derived classes.
What am I missing here?

Cheers
Immanuel



_______________________________________________
Interest mailing list
Interest at qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20161125/c243d617/attachment.html>


More information about the Interest mailing list