Qt 在qml中使用createComponent,但状态始终为错误
当我使用Qt 在qml中使用createComponent,但状态始终为错误,qt,qml,qtquick2,qqmlcomponent,Qt,Qml,Qtquick2,Qqmlcomponent,当我使用Qt.createComponent动态创建组件时,stutas总是component.error,但我无法理解原因 我是这样用的: Rectangle{ function loadTest(){ function finishCreation() { if (component.status === Component.Ready) { console.log("ready") }
Qt.createComponent
动态创建组件时,stutas总是component.error
,但我无法理解原因
我是这样用的:
Rectangle{
function loadTest(){
function finishCreation() {
if (component.status === Component.Ready) {
console.log("ready")
} else if (component.status === Component.Error) {
// Error Handling
console.log("Error loading component:", component.errorString());
}
}
var component = Qt.createComponent("MyPage.qml");
console.log(component.status)
console.log("Error loading component:", component.errorString());
component.statusChanged.connect(finishCreation);
if (component.status === Component.Ready) {
var button = component.createObject(container);
console.log("ready")
}
}
Component.onCompleted: {
console.log("Completed Running!")
loadTest()
}
}
如果qrc
文件中不存在MyPage.qml
,则错误为
qrc:/MyPage.qml:-1未找到文件”
如果我设置了MyPage.qml
的完整路径,就会出现网络错误
当我将SeriesSelectionPage.qml
文件添加到资源文件时,它可以工作。但它不应该是动态的
我只想找到一个QML文件,并在应用程序执行时动态加载它,以便应用程序可以根据用户操作加载不同的QML
有人知道怎么做吗?我快疯了。将url
作为其第一个参数。来自:
url类型引用资源定位器(例如文件名)。它可以是绝对的,例如“”,也可以是相对的,例如“pics/logo.png”。相对url是相对于包含组件的url解析的
因此,只要您在从QRC
文件加载的文件中使用相对URL,您就需要使用QRC
方案:
var component = Qt.createComponent("qrc:/MyPage.qml");
只需将QML文件添加到资源中并使用“qrc:/“URL中的协议。我的印象是OP希望最终用户能够选择QML文件。。。我猜我看错了。是的,你是对的。看起来你的第一个答案更正确:)对不起,不,我认为你是对的,我只是看错了问题!哈哈.)谢谢大家。我很抱歉,我的英语太弱了,表达不清楚。