在编写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
Run Code Online (Sandbox Code Playgroud)
但是,即使我在输出中出现这些错误,我实际上可以在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;
};
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
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();
}
Run Code Online (Sandbox Code Playgroud)
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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我在Arch Linux上使用了Qt Creator 2.7.81的git build.
谢谢你的帮助 !
d
sta*_*low 12
您的警告是因为您在调用时设置并加载QML的源文件:
viewer.setMainQmlFile(QLatin1String("qml/QML_Cpp_propertyTest/main.qml"));
Run Code Online (Sandbox Code Playgroud)
在这个阶段,您的房产的背景是未知的.它只是一个警告,幸运的是,一旦你打电话,QML足够智能解决这个参考错误:
viewer.rootContext()->setContextProperty("textFromQt", QString("My C++ text"));
viewer.rootContext()->setContextProperty("propertyTest", pt);
Run Code Online (Sandbox Code Playgroud)
要在每次打印时停止此警告,必须在加载QML的源文件之前设置上下文属性(即,在setContextProperty方法之前移动setMainQmlFile方法).
我不认为这些警告与您的QML国际化问题有任何关系,但如果没有任何相关的源代码,很难说.如果您仍然遇到QML国际化的困难,我建议发布一个更具针对性的新问题.
| 归档时间: |
|
| 查看次数: |
6621 次 |
| 最近记录: |