一、使用QDeclarativeComponent
QDeclarativeEngine *engine = new QDeclarativeEngine();
QDeclarativeComponent *component = new QDeclarativeComponent(engine,QString::fromLocal8Bit("Face.qml"));
QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(component->create());//获取QML根元素对象
int size = item->height();
二、使用QDeclarativeView
QDeclarativeView view;
view.setSource(QUrl::fromLocalFile("MyItem.qml"));
view.show();
QObject *object = view.rootObject();//获取QML根元素对象
在获取QML根元素对象后,便可使QML与C++进行交互。
问题:QDeclarativeComponent对象使用资源中的qml文件时,出现运行时错误:“QDeclarativeComponent: Component is not ready”。