C ++:
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlEngine engine;
interaction inter("test");
engine.rootContext()->setContextProperty("interaction", &inter);
QQmlComponent component(&engine, QUrl::fromLocalFile("qrc:///main.qml"));
if (component.status() != component.Ready) {
if (component.status() == component.Error) {
qDebug(component.errorString().toUtf8().constData());
}
}
else {
qDebug("not ready");
}
}
component.create();
return app.exec();
}
QML:
import QtQuick 2.2
Rectangle {
width: 500 ; height: 500
visible: true
MouseArea {
anchors.fill: parent
onClicked: {
text.text = inter.author
}
}
Text {
id: text
text: "some text to change"
}
}
错误:
“ file:/// C:/ Qt / Tools / QtCreator / bin / build-testcpp-Desktop_Qt_5_3_MSVC2013_OpenGL_64bit-Debug / qrc:/main.qml:-1找不到文件
QQmlComponent:组件尚未就绪”
我是qt的新手,正尝试使用c ++更改文本元素,它可以编译并运行良好,但由于找不到qrc文件而无法加载。我尝试禁用影子构建,通过完整路径而不是“ qrc:///main.qml”传递QUrl,并且尝试将路径包装在QStringLiteral中,但似乎没有任何效果。
如果有人有任何建议,将不胜感激。
编辑:
.pro
TEMPLATE = app
QT += qml quick
SOURCES += main.cpp \
interaction.cpp
RESOURCES += qml.qrc
# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =
# Default rules for deployment.
include(deployment.pri)
HEADERS += \
interaction.h
.qrc
<RCC>
<qresource prefix="/">
<file>main.qml</file>
<file>MyItem.qml</file>
</qresource>
</RCC>
首先,不要禁用阴影构建。他们不是问题。永远不会。
该qrc
不是指文件系统。它指的是Qt资源系统。该main.qml
文件必须编译到应用程序的可执行文件中。qt资源编译器(qrc)工具可以处理该问题。
因此,您的URL错误。该文件不是本地文件。这是一种资源。只需做:
QQmlComponent component(&engine, QUrl("qrc:/main.qml"));
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句