Error when accessing C++ property or method from QML
Solved
QML and Qt Quick
-
Hi I follow this tutorial but are not able to access property or method defined in C++ class.
I have a C++ class Interface defined:
class Interface : public QObject { Q_OBJECT Q_PROPERTY(QPointF vpose READ getVpose WRITE setVpose NOTIFY vposeChanged) public: explicit Interface(int domainid=0, QObject *parent = nullptr); virtual ~Interface() override; QPointF getVpose() const { return vpose; } void setVpose(const QPointF &p) { if (p != vpose) { vpose = p; emit vposeChanged(); } } Q_INVOKABLE void pushCoordinate(const int &index) { ... } signals: void vposeChanged(); private: QPointF vpose; // vehicle pose int domain_id; };
in my main.cpp:
int main(int argc, char *argv[]) { #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); #endif QGuiApplication app(argc, argv); QQmlApplicationEngine engine; const QUrl url(QStringLiteral("qrc:/main.qml")); QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, &app, [url](QObject *obj, const QUrl &objUrl) { if (!obj && url == objUrl) QCoreApplication::exit(-1); }, Qt::QueuedConnection); Interface interface; engine.rootContext()->setContextProperty("interface", &interface); engine.load(url); return app.exec(); }
In main.qml:
ApplicationWindow { id: appWindow property var map property var parameters property var fromCoordinate//: QtPositioning.coordinate(59.9483, 10.7695) property var toCoordinate//: QtPositioning.coordinate(59.9645, 10.671) property var testcplusplus: interface.vpose // Line 13 here QCreator warning: expected token ";"
I got this error when compiling:
It seems QML does not recognize this interface variable from C++. Did I do something wrong?