The Transition from Qt 4.x to Qt 5

The transition from Qt 4.x to Qt 5 is not expected to be significant. However, the “modularization” of the Qt code base requires some amount of changes to project configuration, such as use of “headers”, and configuration of project build settings (such as changes to the *.pro files).

Qt Creator (master) is compiled using Qt 4 and Qt 5; you can refer to its sources to get an overview of what is required to port an application and keep the sources backwards compatible to Qt 4.

QtWidgets as a Separate Module

example compile time errors 
error: QMainWindow: No such file or directory 
error: QToolButton: No such file or directory 
error: QWidget: No such file or directory 
Solution 
Add this in your *.pro file:

QT += widgets
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
  • 1
  • 2
  • 1
  • 2

Change all instances of

#include <QtGui>
  • 1
  • 1

to

#include <QtWidgets>
  • 1
  • 1

The code should work now, though sometimes you may require to be more explicit:

#include <QtWidgets/QToolButton>
  • 1
  • 1

QtWebKitWidgets is also a separate module:

example compile time errors 
error: invalid use of incomplete type ‘class QWebFrame’ 
error: forward declaration of ‘class QWebFrame’ 
Solution 
Add this in your *.pro file:

QT += webkitwidgets
  • 1
  • 1

Note: when you have QT += webkitwidgets you don’t need QT += widgets

In addition, replace all instances of

#include <QtWebKit>
  • 1
  • 1

to

#include <QtWebKitWidgets>
  • 1
  • 1

You can try this by porting a WYSISWYG html editor [qt.gitorious.org] from Qt 4 to Qt 5.

QPrinter Doesn’t Work

If your code has the following lines:

#include <QPrinter>
#include <QPrintDialog>
  • 1
  • 2
  • 1
  • 2

add the following to your project file:

QT += printsupport
  • 1
  • 1

Again, sometimes it may not work and you would need to be explicit:

#include <QtPrintSupport/QPrinter>
#include <QtPrintSupport/QPrintDialog>
  • 1
  • 2
  • 1
  • 2

toAscii() and fromAscii() Methods are deprecated

Replace all instances of

fromAscii()
toAscii()
  • 1
  • 2
  • 1
  • 2

to

fromLatin1()
toLatin1()
  • 1
  • 2
  • 1
  • 2

For example, given the Qt 4 code

QByteArray configfileti = TMP_Config.toAscii();
  • 1
  • 1

you would change to

QByteArray configfileti = TMP_Config.toLatin1();
  • 1
  • 1

QCoreApplication::UnicodeUTF8 is deprecated

This enum type used to define the 8-bit encoding of character string arguments to translate(). This enum is now obsolete and UTF-8 will be used in all cases. So remove all instances of QCoreApplication::UnicodeUTF8. For example:

Href_Gui->setWindowTitle(QApplication::translate("Href_Gui", "Url / www", 0,QApplication::UnicodeUTF8));
label->setText(QApplication::translate("Href_Gui", "Text:", 0, QApplication::UnicodeUTF8));
label_2->setText(QApplication::translate("Href_Gui", "Url:", 0,QApplication::UnicodeUTF8));
label_3->setText(QApplication::translate("Href_Gui", "Target / Name:", 0,QApplication::UnicodeUTF8));
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

to

Href_Gui->setWindowTitle(QApplication::translate("Href_Gui", "Url / www", 0));
label->setText(QApplication::translate("Href_Gui", "Text:", 0));
label_2->setText(QApplication::translate("Href_Gui", "Url:", 0));
label_3->setText(QApplication::translate("Href_Gui", "Target / Name:", 0));
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

QWorkspace is deprecated

This class is obsolete and was replaced by the QMdiArea class in Qt 4.3. In Qt 5 QWorkspace has been removed. The new class has a similar API to QWorkspace and porting it only involved changing the names of a few methods, signals, and slots.

replace

#include <QWorkspace>
  • 1
  • 1

with

#include <QMdiArea>
  • 1
  • 1

QDrag Problems

Apps that has drop and drag functionality will need some tweaking. A line such as

QDrag *drag = new QDrag(event->widget());
  • 1
  • 1

in Qt 5 will generate the error

error: no matching function for call to ‘QDrag::QDrag(QWidget*)’ 
To fix this add among the includes:

#include <QWidget>
  • 1
  • 1

qFindChildren is deprecated

An error will pop of this fashion: 
error: ‘qFindChildren’ was not declared in this scope 
To solve this you replace qFindChildren with findChildren, for example in

toString(const QObject* obj, int indentLevel) const {
[...]
/* Query over QObjects */
if (m_children) {
QList<QObject*> childlist = qFindChildren<QObject*>(obj, QString());
[...]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

replace

QList<QObject*> childlist = qFindChildren<QObject*>(obj, QString());
  • 1
  • 1

with

QList<QObject*> childlist = obj->findChildren<QObject*>(QString());
  • 1
  • 1

qVariantValue is deprecated

Your compiler will say 
error: ‘qVariantValue’ was not declared in this scope 
This function is equivalent to QVariant::value(value). Therefore if given a QVariant val rewrite the line

QTime t = qVariantValue<QTime>(val);
  • 1
  • 1

to

QTime t = val.value<QTime>();
  • 1
  • 1

This QTime enclosed in the angled brackets lets the compiler know what QVariant will return. However, if the variable is not a QVariable the type enclosed in the angled brackets should not be used(doing so will result in a vague compile time error). So given that m_color is of type QColor you will rewrite

s.setValue("color/favorite", qVariantValue<QColor>(m_color));
  • 1
  • 1

to

s.setValue("color/favorite", m_color.value());
  • 1
  • 1

qVariantCanConvert is deprecated

replace

Q_ASSERT(qVariantCanConvert<QString>(variant));
Q_ASSERT(qVariantCanConvert<QSize>(variant));
Q_ASSERT(qVariantCanConvert<QFont>(fontVariant));
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

with

Q_ASSERT(variant.canConvert(QMetaType::QString));
Q_ASSERT(variant.canConvert(QMetaType::QSize));
Q_ASSERT(fontVariant.canConvert(QMetaType::QFont));
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

Qt::escape is deprecated

error: ‘escape’ is not a member of ‘Qt’ 
So you would change the following block:

    if (result == QString())
result = Qt::escape(val.toString());
else
result = Qt::escape(result);
return result;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

to

    if (result == QString())
result = QString(val.toString()).toHtmlEscaped();
else
result = QString(result).toHtmlEscaped();
return result;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

this procedure can be automated by a porting tool from KDAB.

QDesktopServices::storageLocation deprecated

error: ‘storageLocation’ is not a member of ‘QDesktopServices’ 
error: ‘DataLocation’ is not a member of ‘QDesktopServices’ 
Use QStandardPaths::StandardLocation:

QString path = s.value("db.path",QDesktopServices::storageLocation(QDesktopServices::DataLocation)).toString();
  • 1
  • 1

to

QString path = s.value("db.path",QStandardPaths::standardLocations(QStandardPaths::DataLocation)).toString();
  • 1
  • 2
  • 1
  • 2

CONFIG+=qtestlib is deprecated

If you have the above line in your project file the compiler will warn you in the compile window, nonetheless the code will still run as usual:

Project WARNING: CONFIG+=qtestlib is deprecated. Use QT+=testlib instead.

QWeakPointer quirks

A code block like

quint64 decodedPointer = line.toULongLong();
MetaData* md = reinterpret_cast<MetaData*>(decodedPointer);
QWeakPointer<MetaData> wp(md);
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

results in

error: no matching function for call to ‘QWeakPointer::QWeakPointer(MetaData*&)’ 
To fix this add to the project file:

DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0
  • 1
  • 1

QtConcurrent Library is Missing?

C:\Qt\Qt5.0.2\5.0.2\mingw47_32\include\QtConcurrent\qtconcurrentthreadengine.h:133: error:undefined reference to `_imp___ZN12QtConcurrent16ThreadEngineBaseD2Ev’ 
In Qt 4, QtConcurrent was part of QtCore, so there was no need to include specific headers. This is no longer the case with Qt 5. If your source code have lines like

m_current = QtConcurrent::blockingMappedReduced(slices, functor, stitchReduce,QtConcurrent::UnorderedReduce );
  • 1
  • 1

You will need to include the header:

#include <QtConcurrent/QtConcurrent>
  • 1
  • 1

and add the following line to your project file:

LIBS += -lQt5Concurrent
  • 1
  • 1

Fixing #include<> Headers

A Perl script “fixqt4headers.pl” exists in qtbase/bin/. that should be run on source code using Qt that corrects the #include<> directives for Qt components to also consider the module name.

Plugin loading

The Q_EXPORT_PLUGIN,Q_EXPORT_PLUGIN2 macros have been deprecated in favor of the new Q_PLUGIN_METADATA macro. The advantage of the new system is that it allows Qt to query the metadata for the plugin without actually dlopen’ing it. This greatly improves performance and reliability of the plugin system.

The new Q_PLUGIN_METADATA macro is included next to the Q_OBJECT macro in the QObject derived class that is returned when loading the plugin. It contains the plugins IID and a filename pointing to a json file containing the metadata for the plugin. The json file is compiled into the plugin and does not need to be installed.

An example on how to change your plugins can be found by looking at the patch that changes the Gif image format plugin, see http://qt.gitorious.org/qt/qtbase/commit/963b4c1647299fd023ddbe7c4a25ac404e303c5d .

Deploying to systems without C++11

When Qt is built from source code on a system with C++11 installed, the Qt libraries/frameworks are linked against the system’s C++11 library (libc++). This means that the Qt libraries/frameworks are not deployable to systems without C++11 installed (such as out-of-the-box Mac OS X 10.6). To be able to deploy to systems that only support the older C++ standard (libstdc++), build Qt from source code with the -no-c++11 configure option.

QTimer is no longer accurate to the millisecond by default

QTimer has now 3 accuracy types, with a new default behaviour:

The new default type is Qt::CoarseTimer which, to reduce power/CPU consumption, allow 5% difference between requested time and actual one, and even allow the timer to fire before the requested time. 
The former one is Qt::PreciseTimer (to the millisecond, never before the requested time). 
A third one is Qt::VeryCoarseTimer and allow a 1 second difference

QUrl addQueryItem moved to QUrlQuery

If you have:

QUrl url;
// ...
url.addQueryItem(key, value);
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

You will want to change it to

QUrl url;
QUrlQuery urlQuery;
// ...
urlQuery.addQueryItem(key, value); url.setUrlQuery(urlQuery);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

QAbstractItemModel changes

void reset()
void setRoleNames(const QHash<int, QByteArray> & roleNames)
  • 1
  • 2
  • 1
  • 2
 
http://blog.csdn.net/x356982611/article/details/53257138

由Qt4.x项目移植到Qt5.x需要注意的事项的更多相关文章

  1. [转]将某个Qt4项目升级到Qt5遇到的问题

    原文:http://hi.baidu.com/xchinux/item/9044d8ce986accbb0d0a7b87晚上花了4个小时,将以前的一个项目从Qt 4.8.4-MinGW升级到了Qt5. ...

  2. 将某个Qt4项目升级到Qt5遇到的问题(13条方法)

    本文转载自http://hi.baidu.com/xchinux/item/9044d8ce986accbb0d0a7b87 一.将某个QT4项目改成QT5遇到的问题 该Qt4项目以前是使用Qt4.7 ...

  3. Qt4项目迁移到Qt5问题:greaterThan(QT_MAJOR_VERSION, 4): QT += widgets .

    文章来源:http://blog.csdn.net/ccf19881030/article/details/18220447 问题一:错误:C1083: 无法打开包括文件:"QApplica ...

  4. 将某个Qt4项目升级到Qt5遇到的问题

    本文转载自http://hi.baidu.com/xchinux/item/9044d8ce986accbb0d0a7b87 一.将某个QT4项目改成QT5遇到的问题 该Qt4项目以前是使用Qt4.7 ...

  5. Cocos2dx-3.0版本 从开发环境搭建(Win32)到项目移植Android平台过程详解

    作为重量级的跨平台开发的游戏引擎,Cocos2d-x在现今的手游开发领域占有重要地位.那么问题来了,作为Cocos2dx的学习者,它的可移植特性我们就需要掌握,要不然总觉得少一门技能.然而这个时候各种 ...

  6. Cocos2d-x项目移植到WinRT/Win8小记

    Cocos2d-x项目移植到WinRT/Win8小记 作者: K.C. 日期: 11/17/2013 Date: 2013-11-17 23:33 Title: Cocos2d-x项目移植到WinRT ...

  7. Cocos2d-x项目移植到WP8小记

    Cocos2d-x项目移植到WP8小记 作者: K.C. 日期: 10/24/2013 Date: 2013-10-24 00:33 Title: Cocos2d-x项目移植到WP8小记 Tags: ...

  8. VC6项目移植到VS2008的若干问题——好的代码,从我做起,从如今做起。

    近期,有个项目开发,须要用到曾经项目的代码,只是曾经项目都是VC6下编译通过的,在VS2008下不一定编译通过,能编译通过也不一定能链接成功.以下总结一下我在一个VC6项目移植到VS2008中遇到的一 ...

  9. JSP myecplise项目移植到ecplise

    把myecplise项目移植到ecplise的一些细节: 参考于http://www.cnblogs.com/liushuijinger/p/3396063.html 因为个人需要,需要把JSP项目从 ...

随机推荐

  1. 【解决方法】You must rebuild it with bitcode enabled (Xcode setting ENABLE_BITCODE)

    出现场景: 正常调试是没有问题的,但是在Archive的时候,报出了这个错误. 问题详情: (null): URGENT: all bitcode will be dropped because ‘x ...

  2. [置顶] 【GBT28181开发:SIP协议实践】之设备远程启动

    下面学习的是设备远程控制的流程,和设备信息.设备目录.设备状态的流程差不多,主要是描述的协议字段不同,模拟SPVMN系统向源设备发送远程启动控制指令,记录下交互的消息,详细研究了下: 转载请注明出处: ...

  3. Thread’s start method and run method

    工作中用到了Thread,一开始用错了,仔细研究了一下,稍作整理. 前言,今天写代码居然这样写的 new Thread() { @Override public void run() { System ...

  4. BingMap的GeocodeService进行地理位置正向和反向检索--后台实现

    一.加入GeocodeService的Web服务引用 地理编码服务(GeocodeService)是以WCF技术公布的一个Web服务,地图编码服务提供了以一个有效的物理地址在地图上匹配其相应的地图地址 ...

  5. The Linux device model

    /sys和 /dev的疑问 1./dev 下放的是设备文件,是由应用层mknod创建的文件.假设底层驱动对mknod的设备号有相应的驱动,如open等函数.那么应用层open "/dev/* ...

  6. [原创] ASP.NET WEBAPI 接入微信公众平台 总结,Token验证失败解决办法

    首先,请允许我说一句:shit! 因为这个问题不难,但是网上有关 ASP.NET WEBAPI的资料太少.都是PHP等等的. 我也是在看了某位大神的博客后有启发,一点点研究出来的. 来看正题! 1.微 ...

  7. 支持iOS9 Universal links遇到的问题

    记录为iOS9上的APP支持Universal links遇到的一些问题. 在Web服务器上传apple-app-site-association文件 必须支持HTTPS获取配置文件 文件名后不加.j ...

  8. apache 配置文件管理

    1. Apache配置系统 从整体来看apache的配置系统包括三个部分: (1) 配置文件:比如 httpd.conf   .htaccess (2) 配置指令:在配置文件 httpd.conf  ...

  9. PHP学习笔记12-上传文件

    上传图片文件并在页面上显示出图片 enctype介绍:enctype属性指定将数据发回到服务器时浏览器使用的编码类型. 取值说明: multipart/form-data: 窗体数据被编码为一条消息, ...

  10. Qt实战之开发CSDN下载助手 (2)

    现在,我们正式开工啦.这一篇主要学习下基本的抓包分析.学会协议登录CSDN并制作登陆界面. 准备工具: 一款http抓包工具. 可以是FireBug或者fiddler.这里我们用httpWatch. ...