Transition from Qt 4.x to Qt5

From Qt Wiki
Revision as of 12:45, 23 February 2015 by Maintenance script (talk | contribs)
Jump to: navigation, search

[toc align_right="yes" depth="2"]

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.
h2. QtWidgets as a Separate Module
h3. 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

h3. Solution
Add this in file:

QT = widgets

Change all instances of

#include <QtGui&gt;


#include <QtWidgets&gt;

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

#include <QtWidgets/QToolButton&gt;

h2. QtWebKitWidgets is also a separate module:
h3. example compile time errors

error: invalid use of incomplete type 'class QWebFrame'
error: forward declaration of 'class QWebFrame'

h3. Solution
Add this in your *.pro file:

QT= webkitwidgets

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

In addition, replace all instances of

#include <QtWebKit&gt;


#include <QtWebKitWidgets&gt;

You can try this by porting a "WYSISWYG html editor&quot;: from Qt 4 to Qt 5.

QPrinter Doesn't Work

If your code has the following lines:

#include <QPrinter&gt;
#include <QPrintDialog&gt;

add the following to your project file:

QT = printsupport

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

#include <QtPrintSupport/QPrinter&gt;
#include <QtPrintSupport/QPrintDialog&gt;

h2. toAscii() and fromAscii() Methods are deprecated
Replace all instances of




For example, given the Qt 4 code

QByteArray configfileti = TMP_Config.toAscii();

you would change to

QByteArray configfileti = TMP_Config.toLatin1();

h2. 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&quot;, "Url / www&quot;, 0, QApplication::UnicodeUTF8));
label->setText(QApplication::translate("Href_Gui&quot;, "Text:", 0, QApplication::UnicodeUTF8));
label_2->setText(QApplication::translate("Href_Gui&quot;, "Url:", 0, QApplication::UnicodeUTF8));
label_3->setText(QApplication::translate("Href_Gui&quot;, "Target / Name:", 0, QApplication::UnicodeUTF8));


Href_Gui->setWindowTitle(QApplication::translate("Href_Gui&quot;, "Url / www&quot;, 0));
label->setText(QApplication::translate("Href_Gui&quot;, "Text:", 0));
label_2->setText(QApplication::translate("Href_Gui&quot;, "Url:", 0));
label_3->setText(QApplication::translate("Href_Gui&quot;, "Target / Name:", 0));

h2. 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.
#include <QWorkspace&gt;

#include <QMdiArea&gt;

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

QDrag drag = new QDrag(event->widget());

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&gt;

h2. 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) {
> childlist = qFindChildren&lt;QObject*>(obj, QString());


QList&lt;QObject*> childlist = qFindChildren&lt;QObject*>(obj, QString());


QList&lt;QObject*> childlist = obj->findChildren&lt;QObject*>(QString());

h2. qVariantValue is deprecated
Your compiler will say

error: 'qVariantValue' was not declared in this scope

This function is equivalent to QVariant::value&lt;T&gt;(value). Therefore if given a QVariant val rewrite the line

QTime t = qVariantValue&lt;QTime&gt;(val);


QTime t = val.value&lt;QTime&gt;();

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&quot;, qVariantValue&lt;QColor&gt;(m_color));


s.setValue("color/favorite&quot;, m_color.value());

h2. qVariantCanConvert is deprecated




h2. 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());
result = Qt::escape(result);
return result;


if (result == QString())
result = QString(val.toString()).toHtmlEscaped();
result = QString(result).toHtmlEscaped();
return result;

this procedure can be automated by a "porting tool&quot;: from KDAB.
h2. 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&quot;, QDesktopServices::storageLocation(QDesktopServices::DataLocation)).toString();


QString path = s.value("db.path&quot;, QStandardPaths::standardLocations(QStandardPaths::DataLocation)).toString();

h2. 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&amp;lt;MetaData*&gt;(decodedPointer);
QWeakPointer&amp;lt;MetaData&amp;gt; wp(md);

results in

error: no matching function for call to 'QWeakPointer&amp;lt;MetaData&amp;gt;::QWeakPointer(MetaData*&)'

To fix this add to the project file:


h2. QtConcurrent Library is Missing?

C:5.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 );

You will need to include the header:

#include <QtConcurrent/QtConcurrent&gt;

and add the following line to your project file:

QT= concurrent

Fixing #include&lt;> Headers

A Perl script "; exists in qtbase/bin/. that should be run on source code using Qt that corrects the #include&lt;> 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 .

== Deploying to systems without C+11
When Qt is built from source code on a system with C11 installed, the Qt libraries/frameworks are linked against the system's C11 library (libc). This means that the Qt libraries/frameworks are not deployable to systems without C11 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-c11 configure option.
h2. 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
h2. QUrl addQueryItem moved to QUrlQuery
If you have:

QUrl url;
// …
url.addQueryItem(key, value);

You will want to change it to

QUrl url;
QUrlQuery urlQuery;
// …
urlQuery.addQueryItem(key, value);

h2. QAbstractItemModel changes

void reset()
void setRoleNames(const QHash&lt;int, QByteArray&gt; & roleNames)

both have changed and are now protected.
See "Compatibility Members for QAbstractItemModel&quot;:
h2. Recommended Reading
* "C+ API Changes&quot;:
* "The porting guide&quot;:
* "Porting Desktop Applications from Qt 4 to Qt 5&quot;:
* "Porting from Qt 4 to Qt 5&quot;:
* "Automated porting from Qt 4 to Qt 5&quot;: ==