Porting QML Applications to Qt 5
When porting QML-related code from Qt 4.8 to Qt 5, application developers should be aware that the QML infrastructure has undergone considerable changes in Qt 5. The sections below describe these changes and the impact they have on your existing code.
There are very few changes in the QML language that affect the porting of existing Qt 4.8 QML code to Qt 5. These are:
- Individual file imports no longer work (for example, import "MyType.qml"). Import the containing directory instead.
- It's no longer possible to override signals from the base component.
The QtQuick module has been updated to version 2. All QML applications should update their import statements to use the new version:
import QtQuick 2.3
highlightResizeSpeedproperties have been renamed to highlightMoveVelocity and highlightResizeVelocity, respectively.
- TextInput and TextEdit's
closeSoftwareInputPanel()methods have been removed. Use the new Qt.inputMethod property and call Qt.inputMethod.show() Qt.inputMethod.hide() to show and hide the virtual keyboard.
- XmlListModel has moved into its own module, QtQuick.XmlListModel. Any code that uses the XmlListModel and XmlRole types must import QtQuick.XmlListModel instead.
- The local storage API that enables SQL support has been moved from the QML Global Object into a
QtQuick.LocalStoragesingleton type. Any code that requires the local storage API must import QtQuick.LocalStorage instead. See the Qt Quick Local Storage documentation for examples.
LayoutItemtype has been removed from the
QtQuickmodule as it was specific to the Graphics View framework backend used in Qt Quick 1.
QtQuick 2 includes a number of behavioral changes and you should thoroughly test your applications after porting. These changes will not necessarily lead to run-time errors, but may break certain assumptions in your code. Below are the prominent changes to be aware of when porting your applications.
Item opacity and visibility:
- The input handling details of opacity and visible have changed. An opacity of zero no longer affects input handling, where previously it stopped mouse input. A visibility of false no longer affects keyboard input, but still stops mouse input. The new enabled property stops mouse and keyboard input, but does not affect how or whether the item is rendered. A workaround for applying the old behavior in most cases is to bind enabled to
(visible && opacity > 0.0).
- Previously, if an item was in a positioner (i.e. a Row, Column, Grid and Flow) and the item's
opacitychanged to 0, or its
false, the positioner would remove the item from its layout and collapse the space for that item. In QtQuick 2, this now only happens when an item's
false; the item opacity no longer affects whether the item is laid out. (This is consistent with the existing behavior of ListView and GridView).
- The TextEdit::textFormat property now defaults to
- When Text::textFormat is set to
Text.AutoTextformat, the text object will automatically switch to
- Modifying the Image::sourceSize now fits the image to the size, maintaining aspect ratio.
- For ListView and GridView, the
cacheBufferproperty now has a non-zero default and delegates in the cache buffer are created asynchronously. Also, using a
RightToLeftlayout now also reverses the
- For Loader, the
sourceComponentChangedsignals are now only emitted when their respective properties change value. (Previously Loader emitted both of these signals when either of the relevant properties had changed.)
Qt.labs.particlesmodule has been removed. It is replaced by the fully-fledged QtQuick.Particles module which is an enormous improvement on its predecessor.
Qt.labs.shadersmodule has been removed as the
ShaderEffectItemand ShaderEffectSource types from this module have been moved into the
QtQuickmodule. Note the
ShaderEffectItemtype has been renamed to ShaderEffect.
In Qt 5, all QML applications are rendered with an OpenGL scenegraph architecture rather than the Graphics View framework used in Qt 4. Due to the scale of this architectural change, the C++ API has been extensively restructured and the
QtDeclarative module has been deprecated in favor of two new modules: Qt QML, which implements the QML engine and language infrastructure, and Qt Quick, which implements the visual canvas and scenegraph backend.
All classes that were previously in the
QtDeclarative module have been moved into the Qt QML and Qt Quick modules, and their class names have been changed to reflect their new module locations. The class name changes are as follows:
|Qt QML||Qt Quick|
QT += qml quick
Required header files can then be included:
#include <QtQml/QQmlEngine> #include <QtQuick/QQuickView>
QtDeclarative module is still available to developers as the
Qt Quick 1 module, as discussed below. However, it should not be used for new applications.)
When porting to QQuickItem, note that QDeclarativeItem inherited from QGraphicsItem; in contrast, QQuickItem inherits directly from QObject, and any QGraphicsItem-specific functionality is no longer available. In particular, QQuickItem does not have a
paint() method for performing custom rendering through the QPainter API. Instead, in Qt 5, custom rendering should be performed through the new
QSG* classes to take full advantage of the scene graph. See the Qt Quick Scene Graph documentation details on using these classes.
Alternatively, the QQuickPaintedItem provides a
paint() method and can be used as a convenient way to port QDeclarativeItem-based classes that use the QPainter API. Note this method is less performant than using the
When porting from QDeclarativeView to QQuickView, note that QDeclarativeView inherited from QGraphicsView. In contrast, QQuickView inherits from QQuickWindow and uses the QWindow infrastructure introduced in Qt 5; any QGraphicsView-specific functionality is no longer available.
qmlviewer tool provided for prototyping and testing QML applications in Qt 4.x has been replaced with the
qmlscene tool which integrates with the new scenegraph features in Qt 5. In Qt 5.2, the qml tool was added, intended as the eventual replacement for
All QML plugins should extend QQmlExtensionPlugin in Qt 5.
Additionally, plugins should use the new Qt plugin infrastructure introduced in Qt 5. QML plugins no longer require the Q_EXPORT_PLUGIN2() macro. Instead, they should use the Q_PLUGIN_METADATA() macro within the plugin class declaration.
See the updated Creating C++ Plugins For QML documentation for an overview of creating QML plugins in Qt 5.
For the purposes of porting older applications, the
QtDeclarative module has been available until Qt 5.6 but has been renamed to
Qt Quick 1. Applications that required Qt Quick 1 specific API (e.g. QDeclarativeView or QDeclarativeItem and the Graphics View integration) can use this module. Note that new applications should use the new Qt QML and Qt Quick modules instead.
To use the
Qt Quick 1 module, add "declarative" to your qmake
QT += declarative
Required header files can be included as follows:
#include <QtDeclarative/QDeclarativeView> #include <QtDeclarative/QDeclarativeItem>
© 2023 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.