我希望使用Qt.createQmlObject()函数从example中的QML字符串创建一个QML项,但第一次得到错误:“错误:Qt.createQmlObject():组件未就绪” ,在第二次创建正确的项目时,出了什么问题?
你可以看到 - 我尝试了各种项目:Item,Rectangle,Component(只有一个具有“status”属性)
测试应用程序是: main.cpp中:
#include <QApplication>
#include <QWSServer>
#include <QDeclarativeView>
int main(int argc, char *argv[])
{
QApplication a(argc, argv, QApplication::GuiServer);
QDeclarativeView view;
view.setMinimumSize(100,100);
view.setResizeMode(QDeclarativeView::SizeRootObjectToView);
view.show();
view.setSource(QUrl::fromUserInput("qrc:/createFromStringTest.qml"));
return a.exec();
}
createFromStringTest.qml:
import QtQuick 1.1
Rectangle {
id: rootRectangle
objectName: "rootRectangle"
anchors.centerIn: parent
anchors.fill: parent
color: "gray"
border.width: 5
border.color: "black"
width: 50
height: 50
property int testCount: 0
MouseArea {
anchors.fill: parent
onClicked: {
testCount +=1;
console.log("====================== Runing test "+testCount+" ======================");
tests()
}
}
// what is right?
Item{
id: parentItem
objectName: "parentItem"
Component.onCompleted: {
console.log("parentItem loaded");
}
}
Component {
id: parentComponent
Item {
id: parentComponentItem
Component.onCompleted: {
console.log("parentComponentItem loaded");
}
}
}
property list<Item> parentListItem
property list<Component> parentListComponent
Rectangle {
id: parentRectangle
objectName: "parentRectangle"
Component.onCompleted: {
console.log("parentRectangle loaded");
}
}
Component.onCompleted: {
console.log("rootRectangle loaded ");
}
Component.onDestruction: {
console.log("rootRectangle destroyed ");
}
function tests(){
try{
var newObjectparentItem = Qt.createQmlObject('import QtQuick 1.1; Rectangle {objectName: "dynparentItem";anchors.centerIn: parent; anchors.fill: parent; border.width: 10; border.color: "red";}',parentItem,"parentItem:");
console.log("parentItem OK ");
}catch(e){
console.log("parentItem error: "+e);
}
try{
var newObjectparentComponent = Qt.createQmlObject('import QtQuick 1.1; Rectangle {objectName: "dynparentComponent";anchors.centerIn: parent; anchors.fill: parent; border.width: 10; border.color: "red";}',parentComponent,"parentComponent:");
console.log("parentComponent OK ");
}catch(e){
console.log("parentComponent error: "+e);
}
try{
var newObjectparentComponentItem = Qt.createQmlObject('import QtQuick 1.1; Rectangle {objectName: "dynparentComponentItem";anchors.centerIn: parent; anchors.fill: parent; border.width: 10; border.color: "red";}',parentComponentItem,"parentComponentItem:");
console.log("parentComponentItem OK ");
}catch(e){
console.log("parentComponentItem error: "+e);
}
try{
var newObjectparentListItem = Qt.createQmlObject('import QtQuick 1.1; Rectangle {objectName: "dynparentListItem";anchors.centerIn: parent; anchors.fill: parent; border.width: 10; border.color: "red";}',parentListItem,"parentListItem:");
console.log("parentListItem OK ");
}catch(e){
console.log("parentListItem error: "+e);
}
try{
var newObjectparentListComponent = Qt.createQmlObject('import QtQuick 1.1; Rectangle {objectName: "dynparentListComponent";anchors.centerIn: parent; anchors.fill: parent; border.width: 10; border.color: "red";}',parentListComponent,"parentListComponent:");
console.log("parentListComponent OK ");
}catch(e){
console.log("parentListComponent error: "+e);
}
try{
var newObjectparentRectangle = Qt.createQmlObject('import QtQuick 1.1; Rectangle {objectName: "dynparentRectangle";anchors.centerIn: parent; anchors.fill: parent; border.width: 10; border.color: "red";}',parentRectangle,"parentRectangle:");
console.log("parentRectangle OK ");
}catch(e){
console.log("parentRectangle error: "+e);
}
}
}
输出:
Qml debugging is enabled. Only use this in a safe environment!
rootRectangle loaded
parentRectangle loaded
parentItem loaded
====================== Runing test 1 ======================
parentItem error: Error: Qt.createQmlObject(): Component is not ready
parentComponent error: Error: Qt.createQmlObject(): Component is not ready
parentComponentItem error: ReferenceError: Can't find variable: parentComponentItem
parentListItem error: Error: Qt.createQmlObject(): Missing parent object
parentListComponent error: Error: Qt.createQmlObject(): Missing parent object
parentRectangle error: Error: Qt.createQmlObject(): Component is not ready
====================== Runing test 2 ======================
parentItem OK
parentComponent OK
parentComponentItem error: ReferenceError: Can't find variable: parentComponentItem
parentListItem error: Error: Qt.createQmlObject(): Missing parent object
parentListComponent error: Error: Qt.createQmlObject(): Missing parent object
parentRectangle OK
rootRectangle destroyed
使用Qt 4.8
答案 0 :(得分:0)
对于像您这样的情况,请使用Qt.createComponent动态初始化您使用的组件。初始化组件后,您可以调用Component.CreateObject。或者你可以使用一个parentComponent:parentComponent.CreateObject(QmlItem, "QML properties")
。我通常使用Qt.createQmlObject在QML文件中立即使用QML窗口执行动态操作,并且不为它创建任何新的QML上下文。
请注意,在大多数情况下,您甚至不需要使用上述任何一项,而是操纵一些QML Repeater及其数据模型。这样您就可以提供智能委托来自定义动态创建的项目。但是这个主题不仅仅是你问题的答案。
答案 1 :(得分:0)
函数Qt.createQmlObject(QML字符串,父ID,文件名)的第三个参数出错我的示例中的文件名包含“:”符号 - 没有它按预期工作!
答案 2 :(得分:0)
根据我的经验,在使用Qt.createComponent + Component.createObject动态创建对象时出现“组件未就绪”错误,并且该组件中存在QML错误。
发现错误的一种方法是将组件的实例静态添加到QML应用程序中,以便在运行该应用程序时显示带有行号的错误。