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

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>

Porting QML Applications to Qt 5的更多相关文章

  1. QML官方系列教程——QML Applications

    附网址:http://qt-project.org/doc/qt-5/qmlapplications.html 假设你对Qt的官方demo感兴趣,能够參考本博客的另一个系列Qt5官方demo解析集 每 ...

  2. Debugging QML Applications

    Debugging QML Applications Console API Log console.log, console.debug, console.info, console.warn an ...

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

    以前调试只是QML或者只是C++的QT程序很简单,断点打上,直接debug按钮一点,喝一口水,自然就停在断点了. 这次遇到C++和QML混合的程序,把CONFIG+=declarative_debug ...

  4. 词频统计_输入到文件_update

    /* 输入文件见337.in.txt 输出文件见338.out.txt */ #include <iostream> #include <cctype> #include &l ...

  5. Declarative Widgets is a QML plugin that adds Qt Widgets support to QML

     05.04.2018  Nathan Collins  8 comments FacebookTwitterGoogle+LinkedInEmail Declarative Widgets is a ...

  6. Qt 5.7 > QML

    本文档翻译自Qt官方文档: http://doc.qt.io/qt-5/qtqml-index.html Qt QML Qt QML模块使用QML语言为开发应用与库提供一个框架.它定义并实现了语言与引 ...

  7. Qt(QML)本地化

    Internationalization and Localization with Qt Quick 程序国际化 1) Use qsTr() for all  Literial UI strings ...

  8. Qt 5.7 > Qt Applications

    本文翻译自Qt官方文档: http://doc.qt.io/qt-5/qmlapplications.html QML 应用 QML是声明式语言,它使得用户界面以及交互行为可以被"描述&qu ...

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

    说道QML,不得不先说一下ECMAScript: ECMAScript语言的标准是由Netscape.Sun.微软.Borland等公司基于JavaScript和JScript锤炼.定义出来的. EC ...

随机推荐

  1. (转)Shell分析服务器日志

    一.目录 转载链接:https://mp.weixin.qq.com/s/W1ekSiHgbGInqQ9HmZaJDA 自己的小网站跑在阿里云的ECS上面,偶尔也去分析分析自己网站服务器日志,看看网站 ...

  2. (转)Go语言核心36讲之Go语言入门基础知识

  3. V1-bug Alpha阶段发布说明

    V1-bug Alpha阶段发布说明 欢迎使用WEDO创意社区(部署IP为:60.205.230.0) WHAT'S WEDO 这是一个致力于创意发布.合作开发的网站. 模块 功能 注册 添加新账号 ...

  4. UML-6.2-用例-用例模型/用例/场景关系

    参与者:具有某些行为的人或事物.如上一章中的收银员. |_主要参与者:收银员. |_协助参与者:程序(自动付费.帮收银员验证输入要素) |_幕后参与者:政府等(电子签章取证找公证机构) 用例:一组相关 ...

  5. CDH集群安装配置(四)- mysql 的安装

    安装mysql,并且创建相关的表(只需要在chd1上面安装而且需要root权限)1.1 查看Centos自带mysql是否已经安装 yum list installed | grep mysql 卸载 ...

  6. Tomcat 9内存溢出:"http-apr-8080-Acceptor-0" java.lang.OutOfMemoryError: Direct buffer memory

    Tomcat开启了APR模式,而APR模式会使用堆外内存,关于堆内存可从如下链接了解一下:http://blog.csdn.net/zhouhl_cn/article/details/6573213. ...

  7. Python爬虫:微博粉丝列表

    前言 本来打算做一个关于微博粉丝列表的爬虫,可以统计一下某个微博账号的粉丝里面,僵尸粉(水军)的数量,大V数量. 结果写完爬虫才发现,现在微博只给人看粉丝列表的前5页.......哈哈,好吧.挺无奈的 ...

  8. 浅谈Supermap iClient for JavaScript 弹窗类

    地图作为信息的载体和呈现方式,是GIS的重要组成部分,它是一个浏览信息的窗口,在信息日益发达的今天 ,各种地图应用如雨后春笋一般出现在大众眼前,而不是像以往一样太过局限于专业的领域.而弹窗,是作为地图 ...

  9. gRPC GoLang Test

    gRPC是Google开源的一个高性能.跨语言的RPC框架,基于HTTP2协议,基于protobuf 3.x,基于Netty 4.x +. gRPC与thrift.avro-rpc.WCF等其实在总体 ...

  10. SpagoBI系列----------[01]SpagoBI简介及安装步骤

    商务智能套件SpagoBI提供一个基于J2EE的框架用于管理BI对象如报表.OLAP分析.仪表盘.记分卡以及数据挖掘模型等的开源BI产品.它提供的BI管理器能 够控制.校验.验证与分发这些BI对象. ...