Qt4到Qt5的一些变化以及容易出现的问题分享
1、一个显著的变化是qt5新增 QtWidgets 模块,将 widget 从 QtGui 分离出来来,头文件包含“ QtWidgets ”。如果没注意这个问题,那么用qt5编译老版本的qt程序就会出现错误。
2、例如编译时错误error: QMainWindow: No such file or directory;error: QToolButton: No such file or directory。这个问题需要从三个方面解决:
在*.pro文件里添加:QT += widgets;然后再源文件中改为#include<QtWidgets>,而不是#include <QtGui/QDialog> 。这是一个最容易遇到的问题。
3、如果编译时出现错误error: invalid use of incomplete type 'class QWebFrame;error: forward declaration of 'class QWebFrame'。
这是因为在qt5里QtWebKitWidgets也成为独立的模块。
解决办法:在*.pro文件里添加:QT += webkitwidgets
这时候就不再需要QT += widgets了,也要包含头文件
4、还有一些变化是qt5丢弃了qt4的一些函数,或者是替换了。例如,
toAscii()和fromAscii()这两个函数就不能用了,会出现未定义的错误,而是用
fromLatin1()和toLatin1()来代替他们。
5、还有一个可能出现的错误,error: 'qFindChildren' was not declared in this scope。这是因为qFindChildren函数也被弃用,而用findChildren函数替换。
6、error: 'qVariantValue' was not declared in this scope,这个错误也是经常遇到,最初被则会个问题折磨了好久,qVariantValue函数在qt5里边也被弃用了,若要实现相同的功能,可以用QVariant::value(value)代替。
7、还有一个编码的问题,qt5 发布之时,默认是 utf8 编码 , 这些函数请去掉:
QTextCodec::setCodecForTr(...)/QTextCodec::setCodecForCStrings(...)/QTextCodec::setCodecForLocale(...),大家可以查阅一些书籍和网上的资料来解决问题。