技术在不断进步,新知识也理应不断学习!Qt5的发布带给我无尽的好奇心,然而,受项目影响,一直使用VS2008 + Qt4.8也未曾及时更新。这几天,果断装上VS2010 + Qt5.1,开始研究。Qt4过渡到Qt5不算显著,然而,“模块化”的Qt代码也需要改变项目配置,如使用“headers”,和配置项目构建(如:改变*.pro文件)。

 

QtWidgets作为一个独立的模块

例如编译时错误

  1. error: QMainWindow: No such file or directory
  2. error: QToolButton: No such file or directory
  3. error: QWidget: No such file or directory

解决办法:

在*.pro文件里添加:

  1. QT += widgets

更改

  1. #include

  1. #include

程序现在应该就可以运行了,但是有时可能需要更加明确的包含

  1. #include

QtWebKitWidgets也是一个独立的模块:

例如编译时错误

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

解决办法:

在*.pro文件里添加:

  1. QT += webkitwidgets

注意:当有QT += webkitwidgets的时候,就不再需要QT += widgets

此外,更改

  1. #inclue

  1. #include

打印机不工作

如果你的代码有以下几行:

  1. #include
  2. #include

将以下内容添加到项目文件中:

  1. Qt += printsupport

同样,有时可能仍无法正常工作,需要指定:

  1. #include QPrinter>
  2. #include QPrintDialog>

toAscii()和fromAscii()已被弃用

替换

  1. fromAscii()
  2. toAscii()

  1. fromLatin1()
  2. toLatin1()

例如,给定的Qt4代码

  1. QByteArry configfileti = TMP_Config.toAscii();

变为

  1. QByteArry configfileti = TMP_Config.toLatin1();

QCoreApplication::UnicodeUTF8已被弃用

此枚举类型用于定义8位编码的字符串参数translate()。此枚举现在已经过时,所有的情况将使用UTF-8。所以删除了QCoreApplication::UnicodeUTF8的所有实例。例如:

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

变为

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

QWorkspace已被弃用

这个类已经过时,在Qt4.3中被替换为QMdiArea。在Qt5中QWorkspace已被删除。新的类与QWorkspace有类似的API,移植只涉及改变几个方法、信号和槽的名字。

更换

  1. #include

  1. #include

QDrag问题

拖动功能的应用程序将需要一些调整。如:

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

在Qt5中将产生错误

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

要解决这个附加组件,其中包括:

  1. #include

qFindChildren已被弃用

这种方式会弹出一个错误:

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

为了解决这个问题,将qFindChildren替换为findChildren,例如

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

替换

  1. QList<</span>QObject*> childlist = qFindChildren<</span>QObject*>(obj, QString());

  1. QList<</span>QObject*> childlist = obj->findChildren<</span>QObject*>(QString());

qVariantValue已被弃用

编译器会出现

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

此功能相当于的QVariant::value(value)。因此,如果指定QVariant val应改写

  1. QTime t = qVariantValue<</span>QTime>(val);

  1. QTime t = val.value<</span>QTime>();

QTime用尖括号括起来,则告知编译器QVariant将返回。但是,如果变量不是一个QVariable,则类型用尖括号括起来就不应该被使用(这样做将导致一个模糊的编译时错误)。所以指定的m_color(QColor类型),应改写

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

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

qVariantCanConvert已被弃用

替换

  1. Q_ASSERT(qVariantCanConvert<</span>QString>(variant));
  2. Q_ASSERT(qVariantCanConvert<</span>QSize>(variant));
  3. Q_ASSERT(qVariantCanConvert<</span>QFont>(fontVariant));

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

Qt::escape已被弃用

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

所以应该更改下面代码:

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

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

QDesktopServices::storageLocation已被弃用

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

使用QStandardPaths::StandardLocation,替换

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

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

QtMutimedia替换了Phonon

    音频、视频已不再使用phonon,如果你还在研究phonon,那么你已经out了!好好研究一下QMediaPlayer、QMediaMetaData ...吧!
 

CONFIG += qtestlib已被弃用

如果在项目文件中使用,则编译器会发出警告,尽管如此代码将照常运行:

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

QWeakPointer怪异

如下代码

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

结果

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

为了解决这个问题,将下面代码添加到项目文件:

  1. DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0

QtConcurrent库的失踪了?

  1. C:\Qt\Qt5.0.2\5.0.2\mingw47_32\include\QtConcurrent\qtconcurrentthreadengine.h:133: error: undefined reference to `_imp___ZN12QtConcurrent16ThreadEngineBaseD2Ev'

在Qt4中,QtConcurrent是QtCore的一部分,所以,没有必要包括特定的头。这已不再是用Qt5的情况下。如果源代码如下

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

则将需要包含头:

  1. #include QtConcurrent>

到项目文件,并添加下面一行:

  1. LIBS += -lQt5Concurrent

固定的#include <>头

在qtbase/bin/中存在一个“fixqt4headers.pl”这样的Perl脚本。运行于Qt源代码运行,为Qt组件纠正#include <>指令还要考虑模块名称。

插件加载

Q_EXPORT_PLUGIN,Q_EXPORT_PLUGIN2宏已经过时,新的宏为Q_PLUGIN_METADATA。新系统的优点是,它允许Qt​​来查询元数据的插件没有实际dlopen'ing它。这极大地提高了插件系统的性能和可靠性。

新Q_PLUGIN_METADATA宏包含QObject的派生类中加载插件时返回的Q_OBJECT宏。它包含插件IID并指向一个包含插件元数据的json文件。json文件被编译成插件,并不需要安装。

例如如何改变插件可以通过查找补丁,改变GIF图像格式的插件,请查看:http://qt.gitorious.org/qt/qtbase/commit/963b4c1647299fd023ddbe7c4a25ac404e303c5d.

部署的系统没有使用C++11

当Qt的系统上安装了C++11,建立从源代码的Qt库/框架链接,系统的C++ 11库(libc++)。这意味着Qt库/框架没有部署到没有安装C++11(如out-of-the-box Mac OS X 10.6)的系统。为了能够部署到系统仅支持较旧的C++标准(libstdc++),构建Qt源代码没有C++11配置选项。

Qt4过渡至Qt5的更多相关文章

  1. ubuntu同时安装qt4.8和qt5.7

    这是ubuntu默认安装(从apt安装)的路径和相关文件,建议编译安装到/opt目录下,使用./configure --prefix=/opt/Qt4.8 /usr/share/qt4 /usr/sh ...

  2. 【Qt开发】QT4 升级到 QT5 改动

    QT4 升级到 QT5 改动: PC部分: [改 QTDIR 变量] 在工程根目录下找到 .user 文件 ,  如 InnoTabPlugin.vcxproj.user 修改指向你的 QT5 根目录 ...

  3. Qt4升级到Qt5

    QtWidgets作为一个独立的模块 例如编译时错误 error: QMainWindow: No such file or directory error: QToolButton: No such ...

  4. qt4升级到qt5的一些问题

    由于最近项目使用的qt版本从qt4.8升级到qt5.5,虽然从4到5的变化不大,不过还是有许多的问题需要记录一下,希望可以帮助到更多的人​ 1.由于Qt5将大部分桌面部件移到了Qt Widgets模块 ...

  5. ubuntu同时安装Qt4.8和Qt5.7[认真写每篇博客^-^]

    这是ubuntu默认安装(从apt安装)的路径和相关文件,建议编译安装到/opt目录下. 从APT安装的默认文件夹 以下是我的安装情况,配置为qt4.8为默认. 安装包或源码下载地址: qt4:htt ...

  6. qt4.8转qt5.4

    1.头文件包含    #include <QtGui/QProgressBar>    #include <QtGui/QProgressDialog>    #include ...

  7. qt4代码到qt5代码出错

    1.error C2039: “toAscii”: 不是“QString”的成员 QT5.0.1把这个函数取消掉了,可以用toLatin1这个函数

  8. Qt4升级Qt5注意问题

    Qt4升级Qt5注意问题 Qt4过渡到Qt5的项目一开始就受阻,记录一下遇到的下面的问题 --->编译遇到类似错误: error: QCalendarWidget: No such file o ...

  9. QT4项目升级到QT5遇到的问题和解决方法

    QT4升级到QT5改动: PC部分: [改QTDIR变量] 在工程根目录下找到.user文件, 如InnoTabPlugin.vcxproj.user 修改指向你的QT5根目录: <Proper ...

随机推荐

  1. php读取3389脚本

    <?php $regkey = 'HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server\Wds\rdpwd\Td ...

  2. Css - 文本溢出处理

    http://blog.163.com/yinwei_666/blog/static/2036157320101113102733794/ overflow: hidden;-o-text-overf ...

  3. IOS第五天(1:取消按钮的监听和设置代理textField字数限制)

    ***********取消按钮的监听和设置代理textField字数限制 UITextFieldDelegate #import "HMViewController.h" @int ...

  4. 在Windows上启用LDAPs

    公司的环境比较特殊, Windows server + Linux desktop, 所以我们希望在server端启用LDAP over SSL功能. 当中走了不少弯路, 网上文章也搜了一大堆, 千辛 ...

  5. CodeForces 625A 思维

    题意是说一个人喝酒 有两种办法 买塑料瓶的 a块钱 喝了就没了 或者是买玻璃瓶的b块钱 喝完还能卖了瓶子c块钱 求最多能喝多少瓶 在开始判断一次 a与b-c的关系 即两种方式喝酒的成本 如果a< ...

  6. maven Ubuntu14.04 安装

    参考:linux上安装使用maven 下载链接:官网Download 解压. 在root用户下执行: cd /opt mkdir maven chmod 755 /opt/maven tar -zvx ...

  7. 用 BigDump 工具导入超大 MySQL 数据库备份文件

    用 BigDump 工具导入超大 MySQL 数据库备份文件  创建于 2010-07-01, 周四 00:00  作者 白建鹏 在<Joomla! 1.5 网站防黑9条戒律>这篇文章中, ...

  8. nor flash和nand flash的区别

    NOR和NAND是现在市场上两种主要的非易失闪存技术.Intel于1988年首先开发出NOR flash技术,彻底改变了原先由EPROM和EEPROM一统天下的局面.紧接着,1989年,东芝公司发表了 ...

  9. Decomposing and Redistributing the Statement Method

    Refactoring: Improving the Design of Existing Code Decomposing and Redistributing the Statement Meth ...

  10. Flink - FlinkKafkaConsumer08

      先看 AbstractFetcher 这个可以理解就是,consumer中具体去kafka读数据的线程,一个fetcher可以同时读多个partitions的数据来看看 /** * Base cl ...