Transition from Qt 4.x to Qt5: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
[[Category:Developing_with_Qt]]<br />[[Category:Developing_Qt]]
[[Category:Developing_with_Qt]]
[[Category:Developing_Qt]]


[toc align_right="yes" depth="2"]
[toc align_right="yes" depth="2"]
Line 6: Line 7:


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).
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).
<br />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.
<br />h2. QtWidgets as a Separate Module
<br />h3. example compile time errors<br /><code><br />error: QMainWindow: No such file or directory<br />error: QToolButton: No such file or directory<br />error: QWidget: No such file or directory<br /></code>
<br />h3. Solution
<br />Add this in your'''.pro file:


<code><br />QT ''= widgets<br /></code>
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.
<br />Change all instances of
 
<br /><code><br />#include <QtGui><br /></code>
h2. QtWidgets as a Separate Module
<br />to
 
<br /><code><br />#include <QtWidgets><br /></code>
h3. example compile time errors
<br />The code should work now, though sometimes you may require to be more explicit:
<code>
<br /><code><br />#include <QtWidgets/QToolButton><br /></code>
error: QMainWindow: No such file or directory
<br />h2. QtWebKitWidgets is also a separate module:
error: QToolButton: No such file or directory
<br />h3. example compile time errors<br /><code><br />error: invalid use of incomplete type 'class QWebFrame'<br />error: forward declaration of 'class QWebFrame'<br /></code>
error: QWidget: No such file or directory
<br />h3. Solution
</code>
<br />Add this in your *.pro file:
 
<br /><code><br />QT''= webkitwidgets<br /></code>
h3. Solution
 
Add this in your'''.pro file:
 
<code>
QT ''= widgets
</code>
 
Change all instances of
 
<code>
#include <QtGui>
</code>
 
to
 
<code>
#include <QtWidgets>
</code>
 
The code should work now, though sometimes you may require to be more explicit:
 
<code>
#include <QtWidgets/QToolButton>
</code>
 
h2. QtWebKitWidgets is also a separate module:
 
h3. example compile time errors
<code>
error: invalid use of incomplete type 'class QWebFrame'
error: forward declaration of 'class QWebFrame'
</code>
 
h3. Solution
 
Add this in your *.pro file:
 
<code>
QT''= webkitwidgets
</code>


'''Note''': when you have QT ''= webkitwidgets you don't need QT''= widgets
'''Note''': when you have QT ''= webkitwidgets you don't need QT''= widgets
Line 29: Line 65:
In addition, replace all instances of
In addition, replace all instances of


<code><br />#include <QtWebKit><br /></code>
<code>
#include <QtWebKit>
</code>


to
to


<code><br />#include <QtWebKitWidgets><br /></code>
<code>
#include <QtWebKitWidgets>
</code>


You can try this by porting a "WYSISWYG html editor":http://qt.gitorious.org/qt-labs/graphics-dojo/trees/c8d0c381b994d7417863832929cc4c3f710f2db5/htmleditor from Qt 4 to Qt 5.
You can try this by porting a "WYSISWYG html editor":http://qt.gitorious.org/qt-labs/graphics-dojo/trees/c8d0c381b994d7417863832929cc4c3f710f2db5/htmleditor from Qt 4 to Qt 5.
Line 41: Line 81:
If your code has the following lines:
If your code has the following lines:


<code><br />#include <QPrinter><br />#include <QPrintDialog><br /></code>
<code>
#include <QPrinter>
#include <QPrintDialog>
</code>


add the following to your project file:
add the following to your project file:


<code><br />QT ''= printsupport<br /></code>
<code>
<br />Again, sometimes it may not work and you would need to be explicit:
QT ''= printsupport
<br /><code><br />#include <QtPrintSupport/QPrinter><br />#include <QtPrintSupport/QPrintDialog><br /></code>
</code>
<br />h2. toAscii() and fromAscii() Methods are deprecated
 
<br />Replace all instances of  
Again, sometimes it may not work and you would need to be explicit:
<br /><code><br />fromAscii()<br />toAscii()<br /></code>
 
<br />to<br /><code><br />fromLatin1()<br />toLatin1()<br /></code>
<code>
<br />For example, given the Qt 4 code<br /><code><br />QByteArray configfileti = TMP_Config.toAscii();<br /></code>
#include <QtPrintSupport/QPrinter>
<br />you would change to<br /><code><br />QByteArray configfileti = TMP_Config.toLatin1();<br /></code>
#include <QtPrintSupport/QPrintDialog>
<br />h2. QCoreApplication::UnicodeUTF8 is deprecated
</code>
<br />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:
 
<br /><code><br />Href_Gui->setWindowTitle(QApplication::translate("Href_Gui", "Url / www", 0, QApplication::UnicodeUTF8));<br />label->setText(QApplication::translate("Href_Gui", "Text:", 0, QApplication::UnicodeUTF8));<br />label_2->setText(QApplication::translate("Href_Gui", "Url:", 0, QApplication::UnicodeUTF8));<br />label_3->setText(QApplication::translate("Href_Gui", "Target / Name:", 0, QApplication::UnicodeUTF8));<br /></code>
h2. toAscii() and fromAscii() Methods are deprecated
<br />to
 
<br /><code><br />Href_Gui->setWindowTitle(QApplication::translate("Href_Gui", "Url / www", 0));<br />label->setText(QApplication::translate("Href_Gui", "Text:", 0));<br />label_2->setText(QApplication::translate("Href_Gui", "Url:", 0));<br />label_3->setText(QApplication::translate("Href_Gui", "Target / Name:", 0));<br /></code>
Replace all instances of  
<br />h2. QWorkspace is deprecated
 
<br />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.
<code>
<br />replace
fromAscii()
<br /><code>#include <QWorkspace><br /></code>
toAscii()
<br />with
</code>
<br /><code>#include <QMdiArea><br /></code>
 
<br />h2. QDrag Problems
to
<br />Apps that has drop and drag functionality will need some tweaking. A line such as
<code>
<br /><code><br />QDrag '''drag = new QDrag(event->widget());<br /></code>
fromLatin1()
<br />in Qt 5 will generate the error
toLatin1()
<br /><code><br />error: no matching function for call to 'QDrag::QDrag(QWidget''')'<br /></code>
</code>
<br />To fix this add among the includes:
 
<br /><code><br />#include <QWidget><br /></code>
For example, given the Qt 4 code
<br />h2. qFindChildren is deprecated
<code>
<br />An error will pop of this fashion:<br /><code><br />error: 'qFindChildren' was not declared in this scope<br /></code>
QByteArray configfileti = TMP_Config.toAscii();
<br />To solve this you replace qFindChildren with findChildren, for example in
</code>
<br /><code><br />toString(const QObject* obj, int indentLevel) const {<br />[…]<br /> /* Query over QObjects */<br /> if (m_children) {<br /> QList<QObject'''> childlist = qFindChildren<QObject*>(obj, QString());<br />[…]<br /></code>
<br />replace
<br /><code><br />QList<QObject*> childlist = qFindChildren<QObject*>(obj, QString());<br /></code>
<br />with
<br /><code><br />QList<QObject*> childlist = obj->findChildren<QObject*>(QString());<br /></code>


<br />"source":https://bugs.webkit.org/attachment.cgi?id=82025&amp;amp;action=diff
you would change to
<br />h2. qVariantValue is deprecated
<code>
<br />Your compiler will say<br /><code><br />error: 'qVariantValue' was not declared in this scope<br /></code>
QByteArray configfileti = TMP_Config.toLatin1();
<br />This function is equivalent to QVariant::value<T>(value). Therefore if given a QVariant val rewrite the line<br /><code><br />QTime t = qVariantValue<QTime>(val);<br /></code>
</code>
<br />to
 
<br /><code><br />QTime t = val.value<QTime>();<br /></code>
h2. QCoreApplication::UnicodeUTF8 is deprecated
<br />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
 
<br /><code><br />s.setValue("color/favorite", qVariantValue<QColor>(m_color));<br /></code>
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:
<br />to
 
<br /><code><br />s.setValue("color/favorite", m_color.value());<br /></code>
<code>
<br />"source":http://stackoverflow.com/questions/14919867/qvariantvalue-is-qt-deprecated-what-is-the-replacement
Href_Gui->setWindowTitle(QApplication::translate("Href_Gui", "Url / www", 0, QApplication::UnicodeUTF8));
<br />h2. qVariantCanConvert is deprecated
label->setText(QApplication::translate("Href_Gui", "Text:", 0, QApplication::UnicodeUTF8));
<br />replace  
label_2->setText(QApplication::translate("Href_Gui", "Url:", 0, QApplication::UnicodeUTF8));
<br /><code><br />Q_ASSERT(qVariantCanConvert<QString>(variant));<br />Q_ASSERT(qVariantCanConvert<QSize>(variant));<br />Q_ASSERT(qVariantCanConvert<QFont>(fontVariant));<br /></code>
label_3->setText(QApplication::translate("Href_Gui", "Target / Name:", 0, QApplication::UnicodeUTF8));
<br />with
</code>
<br /><code><br />Q_ASSERT(variant.canConvert(QMetaType::QString));<br />Q_ASSERT(variant.canConvert(QMetaType::QSize));<br />Q_ASSERT(fontVariant.canConvert(QMetaType::QFont));<br /></code>
 
<br />h2. Qt::escape is deprecated
to
<br /><code><br />error: 'escape' is not a member of 'Qt'<br /></code>
 
<br />So you would change the following block:
<code>
<br /><code><br /> if (result == QString())<br /> result = Qt::escape(val.toString());<br /> else<br /> result = Qt::escape(result);<br /> return result;<br /></code>
Href_Gui->setWindowTitle(QApplication::translate("Href_Gui", "Url / www", 0));
<br />to
label->setText(QApplication::translate("Href_Gui", "Text:", 0));
<br /><code><br /> if (result == QString())<br /> result = QString(val.toString()).toHtmlEscaped();<br /> else<br /> result = QString(result).toHtmlEscaped();<br /> return result;<br /></code>
label_2->setText(QApplication::translate("Href_Gui", "Url:", 0));
<br />this procedure can be automated by a "porting tool":http://www.kdab.com/automated-porting-from-qt-4-to-qt-5/ from KDAB.
label_3->setText(QApplication::translate("Href_Gui", "Target / Name:", 0));
<br />h2. QDesktopServices::storageLocation deprecated
</code>
<br /><code><br />error: 'storageLocation' is not a member of 'QDesktopServices'<br />error: 'DataLocation' is not a member of 'QDesktopServices'<br /></code>
 
<br />Use QStandardPaths::StandardLocation:
h2. QWorkspace is deprecated
<br /><code><br />QString path = s.value("db.path", QDesktopServices::storageLocation(QDesktopServices::DataLocation)).toString();<br /></code>
 
<br />to
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.
<br /><code><br />QString path = s.value("db.path", QStandardPaths::standardLocations(QStandardPaths::DataLocation)).toString();<br /></code>
 
<br />"source":http://doc.qt.io/qt-5.0/qtgui/qdesktopservices-obsolete.html
replace
<br />h2. CONFIG''=qtestlib is deprecated
 
<code>#include <QWorkspace>
</code>
 
with
 
<code>#include <QMdiArea>
</code>
 
h2. QDrag Problems
 
Apps that has drop and drag functionality will need some tweaking. A line such as
 
<code>
QDrag '''drag = new QDrag(event->widget());
</code>
 
in Qt 5 will generate the error
 
<code>
error: no matching function for call to 'QDrag::QDrag(QWidget''')'
</code>
 
To fix this add among the includes:
 
<code>
#include <QWidget>
</code>
 
h2. qFindChildren is deprecated
 
An error will pop of this fashion:
<code>
error: 'qFindChildren' was not declared in this scope
</code>
 
To solve this you replace qFindChildren with findChildren, for example in
 
<code>
toString(const QObject* obj, int indentLevel) const {
[…]
/* Query over QObjects */
if (m_children) {
QList<QObject'''> childlist = qFindChildren<QObject*>(obj, QString());
[…]
</code>
 
replace
 
<code>
QList<QObject*> childlist = qFindChildren<QObject*>(obj, QString());
</code>
 
with
 
<code>
QList<QObject*> childlist = obj->findChildren<QObject*>(QString());
</code>
 
 
"source":https://bugs.webkit.org/attachment.cgi?id=82025&amp;amp;action=diff
 
h2. qVariantValue is deprecated
 
Your compiler will say
<code>
error: 'qVariantValue' was not declared in this scope
</code>
 
This function is equivalent to QVariant::value<T>(value). Therefore if given a QVariant val rewrite the line
<code>
QTime t = qVariantValue<QTime>(val);
</code>
 
to
 
<code>
QTime t = val.value<QTime>();
</code>
 
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
 
<code>
s.setValue("color/favorite", qVariantValue<QColor>(m_color));
</code>
 
to
 
<code>
s.setValue("color/favorite", m_color.value());
</code>
 
"source":http://stackoverflow.com/questions/14919867/qvariantvalue-is-qt-deprecated-what-is-the-replacement
 
h2. qVariantCanConvert is deprecated
 
replace  
 
<code>
Q_ASSERT(qVariantCanConvert<QString>(variant));
Q_ASSERT(qVariantCanConvert<QSize>(variant));
Q_ASSERT(qVariantCanConvert<QFont>(fontVariant));
</code>
 
with
 
<code>
Q_ASSERT(variant.canConvert(QMetaType::QString));
Q_ASSERT(variant.canConvert(QMetaType::QSize));
Q_ASSERT(fontVariant.canConvert(QMetaType::QFont));
</code>
 
h2. Qt::escape is deprecated
 
<code>
error: 'escape' is not a member of 'Qt'
</code>
 
So you would change the following block:
 
<code>
if (result == QString())
result = Qt::escape(val.toString());
else
result = Qt::escape(result);
return result;
</code>
 
to
 
<code>
if (result == QString())
result = QString(val.toString()).toHtmlEscaped();
else
result = QString(result).toHtmlEscaped();
return result;
</code>
 
this procedure can be automated by a "porting tool":http://www.kdab.com/automated-porting-from-qt-4-to-qt-5/ from KDAB.
 
h2. QDesktopServices::storageLocation deprecated
 
<code>
error: 'storageLocation' is not a member of 'QDesktopServices'
error: 'DataLocation' is not a member of 'QDesktopServices'
</code>
 
Use QStandardPaths::StandardLocation:
 
<code>
QString path = s.value("db.path", QDesktopServices::storageLocation(QDesktopServices::DataLocation)).toString();
</code>
 
to
 
<code>
QString path = s.value("db.path", QStandardPaths::standardLocations(QStandardPaths::DataLocation)).toString();
</code>
 
"source":http://doc.qt.io/qt-5.0/qtgui/qdesktopservices-obsolete.html
 
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:
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:


<code><br />Project WARNING: CONFIG+=qtestlib is deprecated. Use QT+=testlib instead.<br /></code>
<code>
Project WARNING: CONFIG+=qtestlib is deprecated. Use QT+=testlib instead.
</code>


== QWeakPointer quirks ==
== QWeakPointer quirks ==
Line 121: Line 322:
A code block like
A code block like


<code><br />quint64 decodedPointer = line.toULongLong();<br />MetaData* md = reinterpret_cast&amp;amp;lt;MetaData*>(decodedPointer);<br />QWeakPointer&amp;amp;lt;MetaData&amp;amp;gt; wp(md);<br /></code>
<code>
quint64 decodedPointer = line.toULongLong();
MetaData* md = reinterpret_cast&amp;amp;lt;MetaData*>(decodedPointer);
QWeakPointer&amp;amp;lt;MetaData&amp;amp;gt; wp(md);
</code>


results in
results in


<code><br />error: no matching function for call to 'QWeakPointer&amp;amp;lt;MetaData&amp;amp;gt;::QWeakPointer(MetaData*&amp;)'<br /></code>
<code>
error: no matching function for call to 'QWeakPointer&amp;amp;lt;MetaData&amp;amp;gt;::QWeakPointer(MetaData*&amp;)'
</code>


To fix this add to the project file:
To fix this add to the project file:


<code><br />DEFINES ''= QT_DISABLE_DEPRECATED_BEFORE=0<br /></code>
<code>
<br />"source":http://forum.qt.io/viewthread/27510
DEFINES ''= QT_DISABLE_DEPRECATED_BEFORE=0
<br />h2. QtConcurrent Library is Missing?
</code>
<br /><code><br />C:5.0.2\5.0.2\mingw47_32\include\QtConcurrent\qtconcurrentthreadengine.h:133: error: undefined reference to `_imp___ZN12QtConcurrent16ThreadEngineBaseD2Ev'<br /></code>
 
<br />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
"source":http://forum.qt.io/viewthread/27510
<br /><code><br />m_current = QtConcurrent::blockingMappedReduced(slices, functor, stitchReduce, QtConcurrent::UnorderedReduce );<br /></code>
 
<br />You will need to include the header:
h2. QtConcurrent Library is Missing?
<br /><code><br />#include <QtConcurrent/QtConcurrent><br /></code>
 
<br />and add the following line to your project file:
<code>
<br /><code><br />QT''= concurrent<br /></code>
C:5.0.2\5.0.2\mingw47_32\include\QtConcurrent\qtconcurrentthreadengine.h:133: error: undefined reference to `_imp___ZN12QtConcurrent16ThreadEngineBaseD2Ev'
</code>
 
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
 
<code>
m_current = QtConcurrent::blockingMappedReduced(slices, functor, stitchReduce, QtConcurrent::UnorderedReduce );
</code>
 
You will need to include the header:
 
<code>
#include <QtConcurrent/QtConcurrent>
</code>
 
and add the following line to your project file:
 
<code>
QT''= concurrent
</code>


== Fixing #include<> Headers ==
== Fixing #include<> Headers ==
Line 153: Line 379:


== Deploying to systems without C+''11
== Deploying to systems without C+''11
<br />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.  
 
<br />h2. QTimer is no longer accurate to the millisecond by default
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.  
<br />QTimer has now 3 accuracy types, with a new default behaviour:<br />* 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.<br />* The former one is Qt::PreciseTimer (to the millisecond, never before the requested time).<br />* A third one is Qt::VeryCoarseTimer and allow a 1 second difference
 
<br />h2. QUrl addQueryItem moved to QUrlQuery
h2. QTimer is no longer accurate to the millisecond by default
<br />If you have:<br /><code><br />QUrl url;<br />// …<br />url.addQueryItem(key, value);<br /></code>
 
<br />You will want to change it to<br /><code><br />QUrl url;<br />QUrlQuery urlQuery;<br />// …<br />urlQuery.addQueryItem(key, value);
QTimer has now 3 accuracy types, with a new default behaviour:
<br />url.setUrlQuery(urlQuery);<br /></code>
* 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.
<br />h2. QAbstractItemModel changes
* The former one is Qt::PreciseTimer (to the millisecond, never before the requested time).
<br /><code><br />void reset()<br />void setRoleNames(const QHash<int, QByteArray> &amp; roleNames)<br /></code><br />both have changed and are now protected.
* A third one is Qt::VeryCoarseTimer and allow a 1 second difference
<br />See "Compatibility Members for QAbstractItemModel":http://doc.qt.io/qt-5/qabstractitemmodel-compat.html#reset
 
<br />h2. Recommended Reading
h2. QUrl addQueryItem moved to QUrlQuery
<br />* "C''+ API Changes":http://doc.qt.io/qt-5.0/qtdoc/sourcebreaks.html<br />* "The porting guide":http://doc.qt.io/qt-5.0/qtdoc/portingguide.html<br />* "Porting Desktop Applications from Qt 4 to Qt 5":http://blog.ics.com/2013/01/porting-desktop-applications-from-qt-4-to-qt-5.html#.UVDeLReGqSr<br />* "Porting from Qt 4 to Qt 5":http://www.kdab.com/porting-from-qt-4-to-qt-5/<br />* "Automated porting from Qt 4 to Qt 5":http://www.kdab.com/automated-porting-from-qt-4-to-qt-5/ ==
 
If you have:
<code>
QUrl url;
// …
url.addQueryItem(key, value);
</code>
 
You will want to change it to
<code>
QUrl url;
QUrlQuery urlQuery;
// …
urlQuery.addQueryItem(key, value);
 
url.setUrlQuery(urlQuery);
</code>
 
h2. QAbstractItemModel changes
 
<code>
void reset()
void setRoleNames(const QHash<int, QByteArray> &amp; roleNames)
</code>
both have changed and are now protected.
 
See "Compatibility Members for QAbstractItemModel":http://doc.qt.io/qt-5/qabstractitemmodel-compat.html#reset
 
h2. Recommended Reading
 
* "C''+ API Changes":http://doc.qt.io/qt-5.0/qtdoc/sourcebreaks.html
* "The porting guide":http://doc.qt.io/qt-5.0/qtdoc/portingguide.html
* "Porting Desktop Applications from Qt 4 to Qt 5":http://blog.ics.com/2013/01/porting-desktop-applications-from-qt-4-to-qt-5.html#.UVDeLReGqSr
* "Porting from Qt 4 to Qt 5":http://www.kdab.com/porting-from-qt-4-to-qt-5/
* "Automated porting from Qt 4 to Qt 5":http://www.kdab.com/automated-porting-from-qt-4-to-qt-5/ ==

Revision as of 07:19, 25 February 2015


[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 your.pro file:

QT ''= widgets

Change all instances of

#include <QtGui>

to

#include <QtWidgets>

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

#include <QtWidgets/QToolButton>

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>

to

#include <QtWebKitWidgets>

You can try this by porting a "WYSISWYG html editor":http://qt.gitorious.org/qt-labs/graphics-dojo/trees/c8d0c381b994d7417863832929cc4c3f710f2db5/htmleditor from Qt 4 to Qt 5.

QPrinter Doesn't Work

If your code has the following lines:

#include <QPrinter>
#include <QPrintDialog>

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>
#include <QtPrintSupport/QPrintDialog>

h2. toAscii() and fromAscii() Methods are deprecated

Replace all instances of

fromAscii()
toAscii()

to

fromLatin1()
toLatin1()

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", "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));

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

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.

replace

#include <QWorkspace>

with

#include <QMdiArea>

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>

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) {
 QList<QObject'''> childlist = qFindChildren<QObject*>(obj, QString());
[]

replace

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

with

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


"source":https://bugs.webkit.org/attachment.cgi?id=82025&amp;action=diff

h2. qVariantValue is deprecated

Your compiler will say

error: 'qVariantValue' was not declared in this scope

This function is equivalent to QVariant::value<T>(value). Therefore if given a QVariant val rewrite the line

QTime t = qVariantValue<QTime>(val);

to

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

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

to

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

"source":http://stackoverflow.com/questions/14919867/qvariantvalue-is-qt-deprecated-what-is-the-replacement

h2. qVariantCanConvert is deprecated

replace

Q_ASSERT(qVariantCanConvert<QString>(variant));
Q_ASSERT(qVariantCanConvert<QSize>(variant));
Q_ASSERT(qVariantCanConvert<QFont>(fontVariant));

with

Q_ASSERT(variant.canConvert(QMetaType::QString));
Q_ASSERT(variant.canConvert(QMetaType::QSize));
Q_ASSERT(fontVariant.canConvert(QMetaType::QFont));

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

to

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

this procedure can be automated by a "porting tool":http://www.kdab.com/automated-porting-from-qt-4-to-qt-5/ 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", QDesktopServices::storageLocation(QDesktopServices::DataLocation)).toString();

to

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

"source":http://doc.qt.io/qt-5.0/qtgui/qdesktopservices-obsolete.html

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

results in

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

To fix this add to the project file:

DEFINES ''= QT_DISABLE_DEPRECATED_BEFORE=0

"source":http://forum.qt.io/viewthread/27510

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>

and add the following line to your project file:

QT''= concurrent

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

url.setUrlQuery(urlQuery);

h2. QAbstractItemModel changes

void reset()
void setRoleNames(const QHash<int, QByteArray> &amp; roleNames)

both have changed and are now protected.

See "Compatibility Members for QAbstractItemModel":http://doc.qt.io/qt-5/qabstractitemmodel-compat.html#reset

h2. Recommended Reading