Reputation: 426
While writing a QML App I got troubles binding, resp. accessing, C++ properties with QML, in a Qt Quick 1 Application built with Qt 4.8.1.
Whenever I'd run the application I would get ReferenceError: Can't find variable: ...
.
After searching through documentation, examples and forums, and creating a small QML project to test this behavior, I still can't figure out why I get these errors.
Here is the 'Application Output' I get for my test :
Starting /.../build-QML_Cpp_propertyTest-Qt_4_8_1_in_PATH_System-Debug/QML_Cpp_propertyTest...
Qml debugging is enabled. Only use this in a safe environment!
file:///.../build-QML_Cpp_propertyTest-Qt_4_8_1_in_PATH_System-Debug/qml/QML_Cpp_propertyTest/main.qml:20: ReferenceError: Can't find variable: propertyTest
file:///.../build-QML_Cpp_propertyTest-Qt_4_8_1_in_PATH_System-Debug/qml/QML_Cpp_propertyTest/main.qml:15: ReferenceError: Can't find variable: textFromQt
file:///.../build-QML_Cpp_propertyTest-Qt_4_8_1_in_PATH_System-Debug/qml/QML_Cpp_propertyTest/main.qml:20: ReferenceError: Can't find variable: propertyTest
However, even though I get these errors on the output, I can, in fact, get the values in the QML app. So it does work.
The thing is, I cannot get QML internationalization to work (http://qt-project.org/wiki/How_to_do_dynamic_translation_in_QML) and was wondering if it could be linked to these errors.
And if not, I anyway want to clean these up !
Here's is the code of my test project :
propertytest.h
#include <QObject>
class PropertyTest : public QObject
{
Q_OBJECT
Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged)
public:
explicit PropertyTest(QObject *parent = 0);
QString text();
void setText(const QString &text);
signals:
void textChanged();
public slots:
private:
QString m_text;
};
propertytest.cpp
#include "propertytest.h"
PropertyTest::PropertyTest(QObject *parent) :
QObject(parent)
{
m_text = "My C++ class test text";
}
QString PropertyTest::text()
{
return m_text;
}
void PropertyTest::setText(const QString &text)
{
m_text = text;
}
main.cpp
#include <QApplication>
#include <QDebug>
#include <QDeclarativeContext>
#include "qmlapplicationviewer.h"
#include "propertytest.h"
Q_DECL_EXPORT int main(int argc, char *argv[])
{
QScopedPointer<QApplication> app(createApplication(argc, argv));
PropertyTest *pt = new PropertyTest();
QmlApplicationViewer viewer;
viewer.addImportPath(QLatin1String("modules"));
viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
viewer.setMainQmlFile(QLatin1String("qml/QML_Cpp_propertyTest/main.qml"));
viewer.rootContext()->setContextProperty("textFromQt", QString("My C++ text"));
viewer.rootContext()->setContextProperty("propertyTest", pt);
viewer.showExpanded();
return app->exec();
}
main.qml
import QtQuick 1.1
Rectangle {
width: 360
height: 360
Column {
anchors.centerIn: parent
spacing: 30
Text {
text: qsTr("Hello World")
font.pointSize: 14
}
Text {
text: textFromQt
color: "red"
font.pointSize: 12
}
Text {
text: propertyTest.text
color: "darkGreen"
font.pointSize: 12
}
}
MouseArea {
anchors.fill: parent
onClicked: {
Qt.quit();
}
}
}
I'm using a git build of Qt Creator 2.7.81 on Arch Linux.
Thanks for your help !
D
Upvotes: 8
Views: 7181
Reputation: 10682
The warning you have is because you are setting and loading the source file of the QML when you call:
viewer.setMainQmlFile(QLatin1String("qml/QML_Cpp_propertyTest/main.qml"));
At this stage, the context for your property's are unknown. Its only a warning and luckily QML is smart enough to resolve this reference error once you call:
viewer.rootContext()->setContextProperty("textFromQt", QString("My C++ text"));
viewer.rootContext()->setContextProperty("propertyTest", pt);
To stop this warning from printing every time, you must set the context properties before loading the source file of your QML (ie. Move the setContextProperty
methods before setMainQmlFile
method).
I do not think that these warnings have anything to do with your QML internationalization problems, but its difficult to tell without any relevant source code. I would suggest posting a new more directed question if you are still having difficulties with QML internationalization.
Upvotes: 12