Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QML & C++ property - ReferenceError: Can't find variable

Tags:

c++

qt

qml

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 :

Application Output

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 !


Code

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

like image 528
Danyright Avatar asked Mar 20 '13 15:03

Danyright


People also ask

What is QML used for?

What is QML? QML is a user interface specification and programming language. It allows developers and designers alike to create highly performant, fluidly animated and visually appealing applications.

What is the difference between Qt and QML?

QML is the language; its JavaScript runtime is the custom V4 engine, since Qt 5.2; and Qt Quick is the 2D scene graph and the UI framework based on it. These are all part of the Qt Declarative module, while the technology is no longer called Qt Declarative.

What is QML framework?

The Qt QML module provides a framework for developing applications and libraries with the QML language. It defines and implements the language and engine infrastructure, and provides an API to enable application developers to extend the QML language with custom types and integrate QML code with JavaScript and C++.

What is QML C++?

QML is designed to be easily extensible through C++ code. The classes in the Qt QML module enable QML objects to be loaded and manipulated from C++, and the nature of QML engine's integration with Qt's meta object system enables C++ functionality to be invoked directly from QML.


1 Answers

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.

like image 94
stackunderflow Avatar answered Oct 01 '22 03:10

stackunderflow