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 definedmain.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; }
-
@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. -
@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 .... -
@J-Hilk read OP's code again. The line
testcpp.fnTest(10)
is written in completely different file than whereTestCpp
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.
-
@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.
-
Ah yes, I forgot about that one. ID of the main window is also global.
... it's a mess :-(
-
@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
-
Whaaaat. I had no idea :D
-
@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 definedPerhaps it have nothing to do, but in
main.qml
, you use a component calledTestQml
but, according to your post, the QML file is calledTest.qml
and notTestQml.qml
. It is just a typo? -
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)