[Interest] Creating Q_GADGET from qml
Immanuel Weber
immanuel.weber at gmail.com
Fri Nov 25 10:27:40 CET 2016
Hi Dmitry,
thanks for your reply. I read that and in principal that's what I have done with my class, I forgot to copy the line with registerEqualsComparator()
But out of frustration after I send the mail, I wrote Qt.doubleVector2D() and the error message pointed me to the solution. Qt and QtPositioning in the qml context are singeltons which provide functions, which return the respective classes, like Qt.vector2D() returns a QVector2D and QtPositioning.coordinate() returns a QGeoCoordinate().
I read up the documentation (http://doc.qt.io/qt-5/qqmlengine.html#qmlRegisterSingletonType-2) for qmlRegisterSingletonType() and created a singelton class which has a function doubleVector2D(). After registering that class it works :D
Cheers
Immanuel
Am 25.11.2016 09:57:30 schrieb Dmitry Volosnykh <dmitry.volosnykh at gmail.com>:
Hello, Immanuel!
I think you have missed this documentation page: http://doc.qt.io/qt-5/positioning-cpp-qml.html [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 [mailto: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 [mailto:Interest at qt-project.org]
http://lists.qt-project.org/mailman/listinfo/interest [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/7b66aae6/attachment.html>
More information about the Interest
mailing list