分类: C/C++
2013-12-26 21:42:47
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.
Add this in your *.pro file:
Change all instances of
to
The code should work now, though sometimes you may require to be more explicit:
Add this in your *.pro file:
Note: when you have QT += webkitwidgets you don’t need QT += widgets
In addition, replace all instances of
to
You can try this by porting a [qt.gitorious.org] from Qt 4 to Qt 5.
If your code has the following lines:
add the following to your project file:
Again, sometimes it may not work and you would need to be explicit:
Replace all instances of
to
For example, given the Qt 4 code
you would change to
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:
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.
replace
with
Apps that has drop and drag functionality will need some tweaking. A line such as
in Qt 5 will generate the error
To fix this add among the includes:
An error will pop of this fashion:
To solve this you replace qFindChildren with findChildren, for example in
replace
with
[bugs.webkit.org]
Your compiler will say
This function is equivalent to QVariant::value
to
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
to
[stackoverflow.com]
replace
with
So you would change the following block:
to
this procedure can be automated by a [kdab.com] from KDAB.
Use QStandardPaths::StandardLocation:
to
[qt-project.org]
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:
A code block like
results in
To fix this add to the project file:
[qt-project.org]
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
You will need to include the header:
and add the following line to your project file:
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.
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 .
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 has now 3 accuracy types, with a new default behaviour: