Unsolved ReferenceError: xxx is not defined
-
Hello,
I have a problem with c++ and qml.
When I want to use something from c++ class I got ReferenceError: xxx is not defined.
Also, I tried qmlRegysterType and I got "module is not installed" but I prefer to use this method with setContextProperty.here is my code. It's not all but only important parts.
For test, I just want to set only one text.main.cpp
#include <QGuiApplication> #include <QQmlApplicationEngine> #include <QQmlContext> #include "xmlreader.h" int main(int argc, char *argv[]) { QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv); xmlReader xmlReaderH; QQmlApplicationEngine engine; QQmlContext *ctx = engine.rootContext(); ctx->setContextProperty("xmlReaderCpp", &xmlReaderH); ctx->setContextProperty("appdir", QGuiApplication::applicationDirPath()); 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); engine.load(url); return app.exec(); }
XmlReader.h
#ifndef XMLREADER_H #define XMLREADER_H #include <QObject> #include <QString> #include <QtXml> #include <QFile> class xmlReader: public QObject{ Q_OBJECT Q_PROPERTY(QVariant helloText READ helloText WRITE setHelloText NOTIFY helloTextChanged) private: QDomDocument xmlDoc; QVariant m_helloText; public: explicit xmlReader(QObject *parent = nullptr); QVariant helloText() const; public slots: void setHelloText(QVariant helloText); signals: void helloTextChanged(QVariant helloText); }; #endif // XMLREADER_H
XmlReader.cpp
#include <xmlreader.h> #include <QString> #include <QtXml> #include <QFile> #include <QtDebug> QVariant xmlReader::helloText() const { return m_helloText; } void xmlReader::setHelloText(QVariant helloText) { if (m_helloText == helloText) return; m_helloText = helloText; emit helloTextChanged(m_helloText); } xmlReader::xmlReader(QObject *parent) : QObject(parent) { QString helloText = "hi"; //testing text }
main.qml
import QtQuick 2.9 import QtQuick.Window 2.9 import QtQuick.Controls 2.2 import QtQuick.Layouts 1.3 import Felgo 3.0 App { id: win NavigationStack{ Page{ id: holder anchors.fill: parent navigationBarHidden: true AppText { text: xmlReaderCpp.helloText //there is problem } } }
Thanks for all your suggestions.
-
Hi and welcome to devnet,
What exactly is xxx ?
-
@SGaist
It's exactly ReferenceError: xmlReaderCpp is not defined -
With current code what you pasted, you should to have this issue. This error clearly indicates that xmlReaderCpp is not defined when you load qml file. See your flow.
-
Using a modified main.qml because I don't have Felgo at hand.
Your code is working properly. Just beware that your property will return an invalid variant as you do not initialise
m_helloText
with any text.I'd recommend using a simple dummy qml file to get started just to check that you have everything working correctly with base types before going further.
-
@dheerendra But if I understand it right I using ctx->setContextProperty("xmlReaderCpp", &xmlReaderH) before loading any qml so I don't understand why xmlReaderCpp isn't defined.
-
It is not xmlReader is undefined. Error must be something else. Just copy paste the exact error.
Here your m_helloText is uninitialised. In the constructor you have initialised some other variable.
Please change the constructor statement to
m_helloText = = "Dheerendra";Then everything should work fine.
-
@dheerendra I initialised m_helloText.
But I still got file:/untitled//main.qml:14: ReferenceError: xmlReaderCpp is not defined -
Did you try as I suggested with a simpler main.qml file ?
-
@SGaist yes I tried it but still nothing but maybe I managed it, I haven't try it yet because now I don't have time. Everything in my code is fine but there is a problem with Felgo.
For future viewers try this https://felgo.com/developers/forums/t/cant-integrate-c-in-qml. I will try it tomorrow and if it work I close this topic as solved.But still thanks for helping.
-
You can refer the example here.
I have taken the code from your sample & made it.