Respect Original Works and translations. For reprinting, please maintain the integrity of the article and provide the original author's address in the form of a hyperlink http://blog.csdn.net/changsheng230, so that you can easily ask and correct it.
There are many ways to integrate qml into a qwidget-based UI program. The specific method depends on the features of the existing UI code.
Integration with qwidget-based UI
If you already have a qwidget-based UI, qml widgets can be integrated using qdeclarativeview. Qdeclarativeview is a subclass of qwidget, so you can load it into your UI just like loading other qwidgets. The specific method is to use the qdeclarativeview: setsource () method to load a qml file to the view, and then add this view (that is, qdeclarativeview) to your UI.
QDeclarativeView *qmlView = new QDeclarativeView; qmlView->setSource(QUrl::fromLocalFile("myqml.qml")); QWidget *widget = myExistingWidget(); QVBoxLayout *layout = new QVBoxLayout(widget); widget->addWidget(qmlView);
The disadvantage of this method is that qdelarativeveiw Initialization is slower than qwidget, and more memory is used. If a large number of qdelarativeveiw objects are created, performance may decrease. In this case, it is better to use qml to overwrite your widgets and use main qml widgets to load widgets, replacing qdelarativeveiw abuse.
Note that the uidesign concept of qwidgets is not the same as that of qml, so it is not always a good idea to port the qwidget-based application to qml. If your UI is composed of a few complex and static elements, using qwidgets is a better choice. If your UI is composed of a large number of simple and dynamic elements, qml is your best choice.
Integrate with the qgraphicsview-based UI to add qml widgets to qgraphicsscene
If you already have a UI Based on Graphics View Framework, you can directly integrate QML widgets into your QGraphicsScene. The specific method is to use QDeclarativeComponent to create a QGraphicsObject from the QML file, and add this graphic object to your scene by using QGraphicsScene: addItem, you can also add the parent to a component that already exists with QGraphicsScene. Example:
>
QGraphicsScene* scene = myExistingGraphicsScene(); QDeclarativeEngine *engine = new QDeclarativeEngine; QDeclarativeComponent component(engine, QUrl::fromLocalFile("myqml.qml")); QGraphicsObject *object = qobject_cast(component.create()); scene->addItem(object);
We recommend that you use the following QGraphicsView options to optimize the QML UIs performance:
- QGraphicsView: setOptimizationFlags (QGraphicsView: DontSavePainterState)
- QGraphicsView: setViewportUpdateMode (QGraphicsView: BoundingRectViewportUpdate)
- QGraphicsScene: setItemIndexMethod (QGraphicsScene: NoIndex)
Load the qgraphicswidget object in qml
Another alternative method is to expose your existing QGraphicsWidget object to QML and build your scene in QML. See the graphic layout example to show how to use QGraphicsWidget, QGraphicsLinearLayout, and QGraphicsGridLayout to expose the Qt graphic layout class to QML.
To expose the existing QGraphicsWidget class to QML, use qmlRegisterType (). For more information about how to use the C ++ type in QML, see expand QML in C ++. (For more information, see QML and C ++ hybrid programming)
Source: integrating QML with existing Qt UI code