8

在编写QML应用程序时,我遇到了绑定问题,分别。在使用 Qt 4.8.1 构建的 Qt Quick 1 应用程序中使用 QML访问C++属性。每当我运行应用程序时,我都会得到. 在搜索文档、示例和论坛并创建一个小型 QML 项目来测试此行为后,我仍然无法弄清楚为什么会出现这些错误。这是我为测试获得的“应用程序输出”:ReferenceError: Can't find variable: ...

应用程序输出

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

但是,即使我在输出中得到这些错误,实际上我也可以在 QML 应用程序中得到这些值。所以它确实有效。
问题是,我无法让 QML 国际化工作(http://qt-project.org/wiki/How_to_do_dynamic_translation_in_QML)并且想知道它是否与这些错误有关。
如果没有,我无论如何都想清理这些!


代码

这是我的测试项目的代码:

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;
};


属性测试.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;
}


主文件

#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();
        }
    }
}


我在 Arch Linux 上使用了 Qt Creator 2.7.81 的 git 版本。

谢谢你的帮助 !
D

4

1 回答 1

12

您收到的警告是因为您在调用时正在设置和加载 QML 的源文件:

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.

于 2013-03-20T20:45:24.360 回答