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. Reference Error appears, but the program runs normally.
Forum Updated to NodeBB v4.3 + New Features

Reference Error appears, but the program runs normally.

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
11 Posts 5 Posters 668 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.
  • W Offline
    W Offline
    w.tkm
    wrote on last edited by w.tkm
    #1

    Hi.
    I get Reference Error, but runs without problems
    It's very strange. What could be causing this?

    Error String
    ReferenceError: testcpp is not defined

    main.qml

    import QtQuick 2.15
    import QtQuick.Window 2.15
    import TestCpp 1.0
    
    Window {
        width: 640
        height: 480
        visible: true
        title: qsTr("Hello World")
    
        TestCpp{
            id: testcpp
        }
    
        TestQml{
            id: testqml
        }
    }
    

    Test.qml

    import QtQuick 2.0
    
    Item {
        Rectangle{
            id: testRect
            width: 50
            height: 50
            color: "Red"
        }
    
        Component.onCompleted: {
            testcpp.fnTest(10)
        }
    }
    

    main.cpp

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include "TestCpp.h"
    
    int main(int argc, char *argv[])
    {
    #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    #endif
    
        qmlRegisterType<TestCpp>("TestCpp", 1, 0, "TestCpp");
    
        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);
        engine.load(url);
    
        return app.exec();
    }
    

    TestCpp.h

    #ifndef TESTCPP_H
    #define TESTCPP_H
    
    #include <QObject>
    
    class TestCpp : public QObject
    {
        Q_OBJECT
    public:
        TestCpp();
    public slots:
        void fnTest(int Num);
    };
    #endif // TESTCPP_H
    

    TestCpp.cpp

    #include "TestCpp.h"
    #include <QDebug>
    
    TestCpp::TestCpp()
    {
    
    }
    
    void TestCpp::fnTest(int Num)
    {
        qDebug() << "Get Num: " << Num;
    }
    
    KroMignonK 1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      @w-tkm said in Reference Error appears, but the program runs normally.:

      testcpp.fnTest(10)

      You try to access testcpp ID in a different file than where it is defined. This won't work, the IDs in QML are only in scope in the file where they are defined.

      (Z(:^

      J.HilkJ 1 Reply Last reply
      0
      • sierdzioS sierdzio

        @w-tkm said in Reference Error appears, but the program runs normally.:

        testcpp.fnTest(10)

        You try to access testcpp ID in a different file than where it is defined. This won't work, the IDs in QML are only in scope in the file where they are defined.

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by J.Hilk
        #3

        @sierdzio said in Reference Error appears, but the program runs normally.:

        This won't work, the IDs in QML are only in scope in the file where they are defined.

        thats actually not true, if it is than please correct me.
        I had it, where, if an Id is not found in the local file, it was searched upwards in the parent tree until it found a fitting ID. This was the cause of a very confusing and lengthly debug session on my part.
        I usually give my root element the id root, except that one time, where I didn't ....


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        sierdzioS 1 Reply Last reply
        1
        • J.HilkJ J.Hilk

          @sierdzio said in Reference Error appears, but the program runs normally.:

          This won't work, the IDs in QML are only in scope in the file where they are defined.

          thats actually not true, if it is than please correct me.
          I had it, where, if an Id is not found in the local file, it was searched upwards in the parent tree until it found a fitting ID. This was the cause of a very confusing and lengthly debug session on my part.
          I usually give my root element the id root, except that one time, where I didn't ....

          sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          @J-Hilk read OP's code again. The line testcpp.fnTest(10) is written in completely different file than where TestCpp is instantiated. I'm 100% certain that ID is out of scope (and QML engine tells us that, too!).

          If that line was in the same file - then sure, no problem, ID is visible in the whole file. Otherwise - nope. There is only one exception to this - delegate components can access IDs from other files, but it's pure magic. Better not use it.

          (Z(:^

          J.HilkJ 1 Reply Last reply
          0
          • sierdzioS sierdzio

            @J-Hilk read OP's code again. The line testcpp.fnTest(10) is written in completely different file than where TestCpp is instantiated. I'm 100% certain that ID is out of scope (and QML engine tells us that, too!).

            If that line was in the same file - then sure, no problem, ID is visible in the whole file. Otherwise - nope. There is only one exception to this - delegate components can access IDs from other files, but it's pure magic. Better not use it.

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #5

            @sierdzio said in Reference Error appears, but the program runs normally.:

            read OP's code again. The line testcpp.fnTest(10) is written in completely different file than where TestCpp is instantiated. I'm 100% certain that ID is out of scope (and QML engine tells us that, too!).

            that I don't question, I don't even see an instantiation of Test.qml in the provided code

            There is only one exception to this - delegate components can access IDs from other files, but it's pure magic. Better not use it.

            I meant something like this:

            //main.qml
            import QtQuick 2.12
            import QtQuick.Window 2.12
            
            Window {
                id:mainWindowRoot
                visible: true
                width: 640
                height: 480
            
                signal someRandomSignal()
            
                onSomeRandomSignal: console.log("Signal in mainWindowRoot")
            
                Test1{
            
                }
                Test2{
            
                }
            
            }
            
            
            //test1.qml
            import QtQuick 2.12
            
            Item {
                id: test1
                Timer{
                    running: true
                    interval: 1000
                    repeat: true
                    onTriggered: {
                        console.log("test1 timer")
                        mainWindowRoot.someRandomSignal()
                    }
                }
            }
            
            
            //test2.qml
            import QtQuick 2.12
            
            Item {
                id: test2
                Timer{
                    running: true
                    interval: 1000
                    repeat: true
                    onTriggered: {
                        console.log("test2 timer")
                        mainWindowRoot.someRandomSignal()
                    }
                }
            }
            
            

            output:

            qml: test2 timer
            qml: Signal in mainWindowRoot
            qml: test1 timer
            qml: Signal in mainWindowRoot
            qml: test2 timer
            qml: Signal in mainWindowRoot
            qml: test1 timer
            qml: Signal in mainWindowRoot
            qml: test2 timer
            qml: Signal in mainWindowRoot
            qml: test1 timer
            qml: Signal in mainWindowRoot
            qml: test2 timer
            qml: Signal in mainWindowRoot
            qml: test1 timer
            qml: Signal in mainWindowRoot
            qml: test2 timer
            qml: Signal in mainWindowRoot
            qml: test1 timer
            qml: Signal in mainWindowRoot
            

            magic.


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            1 Reply Last reply
            1
            • sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on last edited by
              #6

              Ah yes, I forgot about that one. ID of the main window is also global.

              ... it's a mess :-(

              (Z(:^

              J.HilkJ 1 Reply Last reply
              0
              • sierdzioS sierdzio

                Ah yes, I forgot about that one. ID of the main window is also global.

                ... it's a mess :-(

                J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by
                #7

                @sierdzio said in Reference Error appears, but the program runs normally.:

                Ah yes, I forgot about that one. ID of the main window is also global.
                ... it's a mess :-(

                !
                that I wasn't even aware off,
                let me adjust my example:

                //main.qml
                import QtQuick 2.12
                import QtQuick.Window 2.12
                
                Window {
                    id:mainWindowRoot
                    visible: true
                    width: 640
                    height: 480
                
                    NotMainWindow{
                
                    }
                
                }  // Window
                
                
                //notmainwindow.qml
                import QtQuick 2.12
                
                Item {
                    id: notMainRoot
                
                    signal someRandomSignal()
                
                    onSomeRandomSignal: console.log("Signal not in mainWindowRoot, but in notMainRoot")
                
                    Test1{
                
                    }
                    Test2{
                
                    }
                }
                
                
                //test1.qml
                import QtQuick 2.12
                
                Item {
                    id: test1
                    Timer{
                        running: true
                        interval: 1000
                        repeat: true
                        onTriggered: {
                            console.log("test1 timer")
                            notMainRoot.someRandomSignal()
                        }
                    }
                }
                
                
                //test2.qml
                import QtQuick 2.12
                
                Item {
                    id: test2
                    Timer{
                        running: true
                        interval: 1000
                        repeat: true
                        onTriggered: {
                            console.log("test2 timer")
                            notMainRoot.someRandomSignal()
                        }
                    }
                }
                
                

                output:

                qml: test2 timer
                qml: Signal not in mainWindowRoot, but in notMainRoot
                qml: test1 timer
                qml: Signal not in mainWindowRoot, but in notMainRoot
                qml: test2 timer
                qml: Signal not in mainWindowRoot, but in notMainRoot
                qml: test1 timer
                qml: Signal not in mainWindowRoot, but in notMainRoot
                qml: test2 timer
                qml: Signal not in mainWindowRoot, but in notMainRoot
                qml: test1 timer
                qml: Signal not in mainWindowRoot, but in notMainRoot
                qml: test2 timer
                qml: Signal not in mainWindowRoot, but in notMainRoot
                qml: test1 timer
                qml: Signal not in mainWindowRoot, but in notMainRoot
                

                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                1 Reply Last reply
                1
                • sierdzioS Offline
                  sierdzioS Offline
                  sierdzio
                  Moderators
                  wrote on last edited by
                  #8

                  Whaaaat. I had no idea :D

                  (Z(:^

                  1 Reply Last reply
                  1
                  • W w.tkm

                    Hi.
                    I get Reference Error, but runs without problems
                    It's very strange. What could be causing this?

                    Error String
                    ReferenceError: testcpp is not defined

                    main.qml

                    import QtQuick 2.15
                    import QtQuick.Window 2.15
                    import TestCpp 1.0
                    
                    Window {
                        width: 640
                        height: 480
                        visible: true
                        title: qsTr("Hello World")
                    
                        TestCpp{
                            id: testcpp
                        }
                    
                        TestQml{
                            id: testqml
                        }
                    }
                    

                    Test.qml

                    import QtQuick 2.0
                    
                    Item {
                        Rectangle{
                            id: testRect
                            width: 50
                            height: 50
                            color: "Red"
                        }
                    
                        Component.onCompleted: {
                            testcpp.fnTest(10)
                        }
                    }
                    

                    main.cpp

                    #include <QGuiApplication>
                    #include <QQmlApplicationEngine>
                    #include "TestCpp.h"
                    
                    int main(int argc, char *argv[])
                    {
                    #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
                        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
                    #endif
                    
                        qmlRegisterType<TestCpp>("TestCpp", 1, 0, "TestCpp");
                    
                        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);
                        engine.load(url);
                    
                        return app.exec();
                    }
                    

                    TestCpp.h

                    #ifndef TESTCPP_H
                    #define TESTCPP_H
                    
                    #include <QObject>
                    
                    class TestCpp : public QObject
                    {
                        Q_OBJECT
                    public:
                        TestCpp();
                    public slots:
                        void fnTest(int Num);
                    };
                    #endif // TESTCPP_H
                    

                    TestCpp.cpp

                    #include "TestCpp.h"
                    #include <QDebug>
                    
                    TestCpp::TestCpp()
                    {
                    
                    }
                    
                    void TestCpp::fnTest(int Num)
                    {
                        qDebug() << "Get Num: " << Num;
                    }
                    
                    KroMignonK Offline
                    KroMignonK Offline
                    KroMignon
                    wrote on last edited by
                    #9

                    @w-tkm said in Reference Error appears, but the program runs normally.:

                    Hi.
                    I get Reference Error, but runs without problems
                    It's very strange. What could be causing this?
                    Error String
                    ReferenceError: testcpp is not defined

                    Perhaps it have nothing to do, but in main.qml, you use a component called TestQml but, according to your post, the QML file is called Test.qml and not TestQml.qml. It is just a typo?

                    It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                    W 1 Reply Last reply
                    0
                    • KH-219DesignK Offline
                      KH-219DesignK Offline
                      KH-219Design
                      wrote on last edited by
                      #10

                      My comment is in regards to "but runs without problems." I would amend that to say "it runs without problems.... for now." Over the long run, I suggest remaining ever-vigilant. But you are vigilant! That's the reason for this post! So good job :)

                      Whenever I see a post about QML warning messages, I feel compelled to repeat my QML "public service announcement" on the topic:

                      My belief: qml "warnings" are best treated as errors. Since QML is running inside a QML/Javascript interpreter hosted in your executable, QML doesn't have the right (the luxury?) to abort the application. So QML's best attempt to get your attention is by printing warnings, which are exceedingly easy to miss. The problem is made worse because QML warnings can appear when there is no user-facing bug happening, so we get lulled into ignoring the warnings as a matter of course. But usually when something finally causes a user-facing bug, there should be a QML warning that is relevant.

                      I have gone to great lengths to squash all QML warnings in my projects. (see: https://forum.qt.io/post/585598)

                      www.219design.com
                      Software | Electrical | Mechanical | Product Design

                      1 Reply Last reply
                      1
                      • KroMignonK KroMignon

                        @w-tkm said in Reference Error appears, but the program runs normally.:

                        Hi.
                        I get Reference Error, but runs without problems
                        It's very strange. What could be causing this?
                        Error String
                        ReferenceError: testcpp is not defined

                        Perhaps it have nothing to do, but in main.qml, you use a component called TestQml but, according to your post, the QML file is called Test.qml and not TestQml.qml. It is just a typo?

                        W Offline
                        W Offline
                        w.tkm
                        wrote on last edited by
                        #11

                        @KroMignon
                        Sorry, It's just a typo...

                        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