繁体   English   中英

如何在c ++中从文件创建自定义QML元素?

[英]How to create custom QML element from file in c++?

我有一个我在我的应用程序中使用的自定义QML元素。

MyImageView.qml

我可以在javascript中使用ComponentDefinition创建它的实例,例如(为了便于阅读而大大简化的名称和方法):

attachedObjects: [
    ComponentDefinition {
        id: defMyImageView
        MyImageView { }
    }
]

function addCustom() {
    var obj = defMyImageView.createObject();
    obj.customSource = "xyz";
    myContainer.add(obj);
}

如何在保留当前MyImageView.qml文件的同时使用c ++执行相同的操作?

经过多次搜索和实验后想出来;)

void ArticleParser::addImage(QString imageUrl, QString title) {
    QmlDocument *document = QmlDocument::create(
            "asset:///Articles/ArticleImage.qml"); //load qml file
    document->setParent(rootContainer); 

    if (!document->hasErrors()) {
        //Create Imageview
        Control *control = document->createRootObject<Control>();
        control->setProperty("source", imageUrl); //custom property
        control->setProperty("caption", title); //custom property

        rootContainer->add(control); //add control to my main container
    }
}

从c ++中调用上述方法,使用我的自定义图像控件(支持http url)添加图像,以便可以动态添加。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM