Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. ReferenceError: xxx is not defined
Forum Updated to NodeBB v4.3 + New Features

ReferenceError: xxx is not defined

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
11 Posts 3 Posters 4.3k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jan Kubice
    wrote on last edited by Jan Kubice
    #1

    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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      What exactly is xxx ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      J 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi and welcome to devnet,

        What exactly is xxx ?

        J Offline
        J Offline
        Jan Kubice
        wrote on last edited by
        #3

        @SGaist
        It's exactly ReferenceError: xmlReaderCpp is not defined

        1 Reply Last reply
        0
        • dheerendraD Offline
          dheerendraD Offline
          dheerendra
          Qt Champions 2022
          wrote on last edited by
          #4

          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.

          Dheerendra
          @Community Service
          Certified Qt Specialist
          http://www.pthinks.com

          J 1 Reply Last reply
          3
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            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.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            1
            • dheerendraD dheerendra

              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.

              J Offline
              J Offline
              Jan Kubice
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • dheerendraD Offline
                dheerendraD Offline
                dheerendra
                Qt Champions 2022
                wrote on last edited by
                #7

                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
                @Community Service
                Certified Qt Specialist
                http://www.pthinks.com

                J 1 Reply Last reply
                0
                • dheerendraD dheerendra

                  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.

                  J Offline
                  J Offline
                  Jan Kubice
                  wrote on last edited by
                  #8

                  @dheerendra I initialised m_helloText.
                  But I still got file:/untitled//main.qml:14: ReferenceError: xmlReaderCpp is not defined

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Did you try as I suggested with a simpler main.qml file ?

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    J 1 Reply Last reply
                    0
                    • SGaistS SGaist

                      Did you try as I suggested with a simpler main.qml file ?

                      J Offline
                      J Offline
                      Jan Kubice
                      wrote on last edited by Jan Kubice
                      #10

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

                      1 Reply Last reply
                      0
                      • dheerendraD Offline
                        dheerendraD Offline
                        dheerendra
                        Qt Champions 2022
                        wrote on last edited by
                        #11

                        You can refer the example here.

                        I have taken the code from your sample & made it.

                        Dheerendra
                        @Community Service
                        Certified Qt Specialist
                        http://www.pthinks.com

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved