Chinaunix首页 | 论坛 | 博客
  • 博客访问: 494361
  • 博文数量: 144
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1190
  • 用 户 组: 普通用户
  • 注册时间: 2013-10-08 20:16
文章分类

全部博文(144)

文章存档

2017年(1)

2015年(5)

2014年(108)

2013年(30)

我的朋友

分类: C/C++

2013-12-26 21:42:47

转自:

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
  1. error: : No such file or directory
  2. error: : No such file or directory
  3. error: : No such file or directory

Solution

Add this in your *.pro file:


  1.  += widgets


Change all instances of


  1. #include


to


  1. #include


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


  1. #include


QtWebKitWidgets is also a separate module:

example compile time errors
  1. error: invalid use of incomplete type 'class QWebFrame'
  2. error: forward declaration of 'class QWebFrame'

Solution

Add this in your *.pro file:


  1.  += webkitwidgets


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

In addition, replace all instances of


  1. #include


to


  1. #include


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

QPrinter Doesn’t Work

If your code has the following lines:


  1. #include
  2. #include


add the following to your project file:


  1.  += printsupport


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


  1. #include
  2. #include


toAscii() and fromAscii() Methods are deprecated

Replace all instances of


  1. fromAscii()
  2. toAscii()


to 

  1. fromLatin1()
  2. toLatin1()


For example, given the Qt 4 code

  1.  configfileti = TMP_Config.toAscii();


you would change to 

  1.  configfileti = TMP_Config.toLatin1();


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:


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


to


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


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


  1. #include


with


  1. #include


QDrag Problems

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


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


in Qt 5 will generate the error


  1. error: no matching function for call to 'QDrag::QDrag(QWidget*)'


To fix this add among the includes:


  1. #include


qFindChildren is deprecated

An error will pop of this fashion:

  1. error: 'qFindChildren' was not declared in this scope


To solve this you replace qFindChildren with findChildren, for example in


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


replace


  1. <*> childlist = qFindChildren<*>(obj, ());


with


  1. <*> childlist = obj->findChildren<*>(());


 [bugs.webkit.org]

qVariantValue is deprecated

Your compiler will say

  1. 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

  1.  t = qVariantValue<>(val);


to


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


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


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


to


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


 [stackoverflow.com]

qVariantCanConvert is deprecated

replace


  1. Q_ASSERT(qVariantCanConvert<>(variant));
  2. Q_ASSERT(qVariantCanConvert<>(variant));
  3. Q_ASSERT(qVariantCanConvert<>(fontVariant));


with


  1. Q_ASSERT(variant.canConvert(::));
  2. Q_ASSERT(variant.canConvert(::));
  3. Q_ASSERT(fontVariant.canConvert(::));


Qt::escape is deprecated


  1. error: 'escape' is not a member of 'Qt'


So you would change the following block:


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


to


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


this procedure can be automated by a  [kdab.com] from KDAB.

QDesktopServices::storageLocation deprecated


  1. error: 'storageLocation' is not a member of 'QDesktopServices'
  2. error: 'DataLocation' is not a member of 'QDesktopServices'


Use QStandardPaths::StandardLocation:


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


to


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


 [qt-project.org]

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:


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


QWeakPointer quirks

A code block like


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


results in


  1. error: no matching function for call to 'QWeakPointer::QWeakPointer(MetaData*&)'


To fix this add to the project file:


  1. DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0


 [qt-project.org]

QtConcurrent Library is Missing?


  1. C:\\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


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


You will need to include the header:


  1. #include


and add the following line to your project file:


  1. LIBS += -lQt5Concurrent


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  .

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


Recommended Reading

Categories:

阅读(3617) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~