Porting C++ Applications to Qt 5
This topic talks about the Qt Widgets and Qt WebKit changes in Qt 5. The following step-by-step instructions take you through the changes required to port the Animated Tiles application to Qt 5:
- Open the Animated Tiles project using Qt Creator.
- Edit
main.cpp
and replace the#include <QtGui>
instance with#include <QtWidgets>
. The Perl-scriptfixqt4headers.pl
can be used to scan the source files of a project and perform the replacements. - Edit the
animatedtiles.pro
and addQT += widgets
towards the end of the file.Note: Qt GUI is included by default in all Qt applications unless excluded using the
QT -= gui
directive in theqmake
project file. - Save the changes and run the application.
Once you see the application running, check whether it behaves as expected.
A similar change is needed to port Qt 4 C++ applications using Qt WebKit. The following step-by-step instructions take you through the changes required to achieve this:
- Open your project using Qt Creator.
- Replace all
#include <QtWebKit>
instances with#include <QtWebKitWidgets>
in your project source. - Edit the
.pro
file and addQT += webkitwidgets
.Note: If the
QT +=
statement already exists, appendwebkitwidgets
to it. - Save changes and run your application.
For larger projects, we recommend initially enabling deprecated API by adding the define
DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0
to the .pro
file. In a second step, the define can be removed.
It is possible to keep the project compiling with Qt 4 and Qt 5. This requires:
- Omitting the module name from all includes. This is done by passing the command line option
--strip-modules
tofixqt4headers.pl
. - Adding scopes depending on the version of Qt to the
.pro
files:greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
- Introducing
#if
scopes around code using modified API:#if QT_VERSION >= 0x050000 headerView->setSectionResizeMode(QHeaderView::ResizeToContents); #else headerView->setResizeMode(QHeaderView::ResizeToContents); #endif