对于Qt4项目与Qt5项目移植时问题及解决方法

 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’

原因:一般时由于Makefiletarget那一行有多余的冒号(:),冒号在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

}

posted on 2019-02-26 17:48  muc_风吹雨  阅读(3448)  评论(0编辑  收藏  举报

导航