1、Qt4項目遷移至Qt5項目:提示error:#include <QtGui/QApplication> No such file or directory
原因:由於Qt5y源文件位置的改動.
解決方法:
①在.pro文件里,將Qt += core gui改為Qt += core gui widgets
②.h文件里,#include <QtGui/QApplication>改為#include <QApplication>
2、Qt4項目遷移至Qt5項目:提示error:’setCodecForTr’ is not a member of ‘QTextCodec’等錯誤.
原因:由於Qt版本更新速度快,更新的同時有了許多的改動,在Qt5版本以前,很多人習慣用下面這三行(只要牽扯到中文,這三行就來了):
QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
但是在Qt5中取消了:
QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
解決方法:
在程序中,main函數里將這兩行代碼注釋掉就可以了.
3、makefile 提示’multiple target patterns. Stop’
原因:一般時由於Makefile中target那一行有多余的冒號(:),冒號在makefile中的作用是用於標識前面時一個編譯的目標,如果有多余的冒號就會報錯.
解決方案:
將程序中多余的冒號去掉即可.
4、構建項目時報錯:’QObject& QObject::(const QObject&)’ is private within this context.
原因 :定義的是個QObject的子類,當將其的實例放入QT的容器(比如QList QHash等),就會報上面的錯誤.簡單的說就是QObject的拷貝構造函數時私有的,當把其子類放入容器時無法完成構造其副本.
解決方案:
使用指針,將自定義的子類以指針的形式保存在容器中.比如:QList<MyObject *>list.
5 qt5.x轉為Qt4.8問題error:QtWidget/QAction:No such file or directory
解決方法:將QtWidget換成QtGui
6 qt5.x轉為Qt4.8問題error:’class QHeader View’ has no member named ‘setSectionResizeMode’
解決方法:是因為TableView之中的問題,在Qt5之后講setResizeMode改成了setSectionResizeMode所以將section去掉即可
7 qt5.x轉為Qt4.8問題error:’itoa’ was not declared in this scope
解決方法:itoa並非標准類庫,將其換成sprintf(tmp,”%d”,i+1);用sprintf即可.
8 qt5.x轉為Qt4.8問題error:’QStingLiteral’ was not declared in this scope
解決方法:問題出在TCPServerWindow->setObjectName(QStringLiteral(“TCPServerWindow”));這句話QT 4.8 setObjectName 沒有QStringLiteral這個 形參是QSTring 所以去掉這個QStringLiteral即可
9 qt5.x轉為Qt4.8問題error:‘Qt_5_6’ is not a member of ‘QDataStream’
解決方法:
當然Qt4.8怎么會有5.6 改成4.8以下即可
10 構建項目時報錯:error: 'staticMetaObject' is not a member of 'ygbaseprotocol'
解決方法:
在定義類時增加繼承自QObject,如下:
class ygbaseprotocol:public QObject
{
Q_OBJECT
}