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.

This article describes the changes that affect your existing code. If you are interested in the summary of all new features in Qt 5 for QML application development, see Qt QML Release Notes and Qt Quick Release Notes.

QML Language changes

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.

  • Relative file paths in JavaScript files are now resolved relative to the location of the JavaScript file instead of the QML file that imported it.
  • It‘s no longer possible to override signals from the base component.

QtQuick Module

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
Property and Method Changes
  • ListView‘s highlightMoveSpeed and highlightResizeSpeed properties have been renamed to highlightMoveVelocity and highlightResizeVelocity, respectively.

  • TextInput and TextEdit‘s openSoftwareInputPanel() and 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.
Type and API Changes
  • 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.LocalStorage singleton type. Any code that requires the local storage API must import QtQuick.LocalStorage instead. See theQt Quick Local Storage documentation for examples.
  • The LayoutItem type has been removed from the QtQuick module as it was specific to the Graphics View framework backend used in Qt Quick 1.
Behavioral Changes

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 opacity changed to 0, or its visible value became 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 visible is false; the item opacity no longer affects whether the item is laid out. (This is consistent with the existing behavior of ListView and GridView).

Text:

  • The TextEdit::textFormat property now defaults to PlainText instead of AutoText.

  • When Text::textFormat is set to Text.AutoText format, the text object will automatically switch to Text.StyledText instead of Text.RichText.

Other:

  • Modifying the Image::sourceSize now fits the image to the size, maintaining aspect ratio.

  • For ListView and GridView, the cacheBuffer property now has a non-zero default and delegates in the cache buffer are created asynchronously. Also, using a RightToLeft layout now also reverses the preferredHighlightBegin andpreferredHighlightEnd.
  • For Loader, the sourceChanged and sourceComponentChanged signals 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.)
Changes to experimental Qt.labs modules
  • The Qt.labs.particles module has been removed. It is replaced by the fully-fledged QtQuick.Particles module which is an enormous improvement on its predecessor.

  • The Qt.labs.shaders module has been removed as the ShaderEffectItem and ShaderEffectSource types from this module have been moved into the QtQuick module. Note the ShaderEffectItem type has been renamed toShaderEffect.

C++ code

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 theQtDeclarative 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

  • QDeclarativeComponent -> QQmlComponent

  • QDeclarativeContext -> QQmlContext
  • QDeclarativeEngine -> QQmlEngine
  • QDeclarativeError -> QQmlError
  • QDeclarativeExpression -> QQmlExpression
  • QDeclarativeExtensionPlugin -> QQmlExtensionPlugin
  • QDeclarativeInfo -> QQmlInfo
  • QDeclarativeListReference -> QQmlListReference
  • QDeclarativeNetworkAccessManagerFactory -> QQmlNetworkAccessManagerFactory
  • QDeclarativeParserStatus -> QQmlParserStatus
  • QDeclarativeProperty -> QQmlProperty
  • QDeclarativePropertyMap -> QQmlPropertyMap
  • QDeclarativePropertyValueSource -> QQmlPropertyValueSource
  • QDeclarativeScriptString -> QQmlScriptString
  • QDeclarativeItem -> QQuickItem

  • QDeclarativeView -> QQuickView
  • QDeclarativeImageProvider -> QQuickImageProvider

To use the new QQml* and QQuick* classes in Qt 5, link against the approprate module from your qmake .pro file. For example the following will link against both the Qt QML and Qt Quick modules:

QT += qml quick

Required header files can then be included:

#include <QtQml/QQmlEngine>
#include <QtQuick/QQuickView>

(The 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.)

QDeclarativeItem and QDeclarativeView

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 Graphdocumentation 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 QSG* classes.

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.

qmlscene Utility

The 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.

QML plugins

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.

QtDeclarative module in Qt 5

For the purposes of porting older applications, the QtDeclarative module is still available in Qt 5 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 .pro file:

QT += declarative

Required header files can be included as follows:

#include <QtDeclarative/QDeclarativeView>
#include <QtDeclarative/QDeclarativeItem>
时间: 2024-10-07 04:55:23

Porting QML Applications to Qt 5的相关文章

Qt官方教程翻译——QML Applications

附网址:http://qt-project.org/doc/qt-5/qmlapplications.html QML Applications QML是一种声明式语言,它提供了一组接口用来描述视觉组件以及他们的互动和相关性.它是一个高度可读的语言,并且被设计成使组件以一个动态的方式相互连接.同时它使组件很容易被复用以及创建定制的用户界面.使用QtQuick模块,设计者和开发者可以很容易使用QML建立流体动画的用户界面,并将这些界面连接到后端的C++库上面. What is QML? QML是一

QML官方系列教程——QML Applications

附网址:http://qt-project.org/doc/qt-5/qmlapplications.html 如果你对Qt的官方demo感兴趣,可以参考本博客的另一个系列Qt5官方demo解析集 每个绿色字体均是一篇博文连接,请收藏本文,本文会持续更新 . QML Applications —— QML应用程序 QML是一种声明式语言,它提供了一组接口用来描述可视化组件以及他们之间的互动.它是一个高度可读的语言,并且被设计成使组件以一个动态的方式相互连接.同时它使组件很容易被复用以及创建定制的

Debugging QML Applications

Debugging QML Applications Console API Log console.log, console.debug, console.info, console.warn and console.error can be used to print debugging information to the console. For example: function f(a, b) { console.log("a is ", a, "b is &qu

QML官方教程——Qt Quick Layouts Overview

附网址:http://qt-project.org/doc/qt-5/qtquicklayouts-overview.html Qt Quick Layouts Overview -- Qt Quick Layouts概述 Qt Quick Layouts是用来对用户界面中的组件进行布局的一套组件.由于Qt Quick Layouts会根据实际情况动态调整其内部组件的尺寸,因此它非常适合被用在尺寸可变的用户界面中. Getting Started 在.qml文件里使用如下声明将其引入到你的应用程

C++和QML混合的QT程序调试方法

以前调试只是QML或者只是C++的QT程序很简单,断点打上,直接debug按钮一点,喝一口水,自然就停在断点了. 这次遇到C++和QML混合的程序,把CONFIG+=declarative_debugCONFIG+=qml_debug配置上点了debug就一直等啊等, 就是不弹出主窗口,不知道的, 估计以为电脑卡死了.就算喝完一桶水,也等不到进断点的时候.打开调试信息面板,会看到提示信息 QML Debugger: Waiting for connection on port xxx,一直wai

Qt官方教程翻译——First Steps with QML

附网址:http://qt-project.org/doc/qt-5/qmlfirststeps.html Creating a QML Document 一个QML文件定义了对象的层次结构:具有高度可读的,结构化的布局.每个QML文件由两部分组成:一个引入(import)部分,以及一个对象声明(declaration)部分.用户界面中最常用的的类型(types)和功能由引入QtQuick提供. Importing and Using the QtQuick Module 为了使用Qt Quic

Qt Quick 之 QML 与 C++ 混合编程详解

Qt Quick 技术的引入,使得你能够快速构建 UI ,具有动画.各种绚丽效果的 UI 都不在话下.但它不是万能的,也有很多局限性,原来 Qt 的一些技术,比如低阶的网络编程如 QTcpSocket ,多线程,又如 XML 文档处理类库 QXmlStreamReader / QXmlStreamWriter 等等,在 QML 中要么不可用,要么用起来不方便,所以呢,很多时候我们是会基于这样的原则来混合使用 QML 和 C++: QML 构建界面, C++ 实现非界面的业务逻辑和复杂运算. 请给

Qt 5.7 &gt; Qt Applications

本文翻译自Qt官方文档: http://doc.qt.io/qt-5/qmlapplications.html QML 应用 QML是声明式语言,它使得用户界面以及交互行为可以被"描述"出来.这是一种可读性非常高.动态互联其中的构件的语言,并且它允许构件可以非常容易的被重用和界面自定义.使用QtQuick模块,设计者和开发者可以使用QML开发流畅的动画界面,也可以选择将用户界面与后端的C++库连接起来. 什么是QML? QML是用户界面规范与编程语言.它使得开发者和设计者可以开发出高性

Qt Quick编程(1)——QML的核心部分ECMAScript

说道QML,不得不先说一下ECMAScript: ECMAScript语言的标准是由Netscape.Sun.微软.Borland等公司基于JavaScript和JScript锤炼.定义出来的. ECMAScript可以为不同种类的宿主环境提供核心的脚本编程能力.ECMAScript仅仅是一个描述,定义了脚本语言的所有属性.方法和对象.它描述了一下内容: 语法 类型 语句 关键字 保留字 运算符 对象 其他语言可以以它为基础拓展出新特性,比如QML引入了Qt对象系统中的信号与槽等特色功能. QM