Qt4.8升级Qt5.9版本问题总结

最好先在.pro文件中加上 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
建议最好和源码对比看着升级,有些问题不看源码,报出来的问题会误导我们。

1、106: error: 'class QString' has no member named 'toAscii'
     qDebug() << sql.toAscii().data();
解决:用toLatin1或者qPrintable()替换

2、507: error: 'fromAscii' is not a member of 'QString'
         fireAlarm.address = QString::fromAscii(secondCode.toHex().data(), secondCode.toHex().size());
                             ^                             
3、-1: In member function 'void CGraphicsItem::rotate(qreal, QPointF)':
832: error: 'translate' was not declared in this scope
     translate(dx, dy);
     或者
   282: error: 'class CGraphicsItem' has no member named 'translate'
   item->translate(point.x(), point.y());

 更改为:item->tranform().translate(point.x(), point.y());可以编译通过,起不起作用还不清楚

Qt5:代码说明

#if QT_DEPRECATED_SINCE(5, 0)
    QT_DEPRECATED inline void rotate(qreal angle) { setTransform(QTransform().rotate(angle), true); }
    QT_DEPRECATED inline void scale(qreal sx, qreal sy) { setTransform(QTransform::fromScale(sx, sy), true); }
    QT_DEPRECATED inline void shear(qreal sh, qreal sv) { setTransform(QTransform().shear(sh, sv), true); }
    QT_DEPRECATED inline void translate(qreal dx, qreal dy) { setTransform(QTransform::fromTranslate(dx, dy), true); }
#endif

4、833: error: 'rotate' is not a member of 'QGraphicsItem'
     QGraphicsItem::rotate(angle); 解决办法同上
     
5、952: error: no matching function for call to 'CGraphicsScene::itemAt(QPointF)'
         QGraphicsItem* item = this->itemAt(event->scenePos());
    查找itemAt()函数的定义,QT5中其定义为QGraphicsItem * QGraphicsScene::itemAt(const QPointF & position, const QTransform & deviceTransform) const,注意其定义与QT4不一样。 
Qt4:
    QGraphicsItem *itemAt(const QPointF &pos) const; // ### obsolete
    QGraphicsItem *itemAt(const QPointF &pos, const QTransform &deviceTransform) const;
Qt5:
    #if QT_DEPRECATED_SINCE(5, 0)
        QT_DEPRECATED inline QGraphicsItem *itemAt(const QPointF &position) const {
            QList<QGraphicsItem *> itemsAtPoint = items(position);
            return itemsAtPoint.isEmpty() ? Q_NULLPTR : itemsAtPoint.first();
        }
    #endif
        QGraphicsItem *itemAt(const QPointF &pos, const QTransform &deviceTransform) const;    ^
更改为如下:
    QTransform transform;
    QGraphicsItem *item = scene()->itemAt(scenePos,transform);

6、152: error: invalid use of incomplete type 'const class QMimeData'
     if (event->mimeData()->hasFormat("device/ts-device")
    添加QMimeData头文件
 
 7、9: error: expected class-name before '{' token
 {
 意思是找不到相关类
 ^添加对应类的头文件
 
 8、55: error: invalid conversion from 'WId {aka unsigned int}' to 'HWND {aka HWND__*}' [-fpermissive]
         
    原码:
    SetWindowLong(winId(), GWL_EXSTYLE, GetWindowLong(winId(), GWL_EXSTYLE) |
                      WS_EX_TRANSPARENT | WS_EX_LAYERED);
    加(HWND)强转后:
    SetWindowLong((HWND)winId(), GWL_EXSTYLE, GetWindowLong((HWND)winId(), GWL_EXSTYLE) |
       WS_EX_TRANSPARENT | WS_EX_LAYERED);
                                                 
9、:-1: error: error: CreateProcess: No such file or directory    
    执行qmake重新编译即可
    
10、4: error: QtGui/QPushButton: No such file or directory
    原:#include <QtGui/QApplication>
    改为:#include <QApplication>
11、156: error: 'UnicodeUTF8' is not a member of 'QApplication'
    原因是:QCoreApplication::UnicodeUTF8已被弃用
    删掉  dlgUserInfo->setWindowTitle(QApplication::translate("dlgUserInfo", "Dialog", 0, QApplication::UnicodeUTF8));中的QApplication::UnicodeUTF8项即可       
12、10: error: 'class QHeaderView' has no member named 'setResizeMode'
     horizontalHeader()->setResizeMode(QHeaderView::Interactive);
     替换 setResizeMode 方法为 setSectionResizeMode
13、142: error: 'qInstallMsgHandler' was not declared in this scope
         qInstallMsgHandler(ConsoleDebugMessageOutput);
         This method is deprecated, use qInstallMessageHandler instead.
14、52: error: variable 'QDataStream write' has initializer but incomplete type
     QDataStream write(&ByteData, QIODevice::WriteOnly);
                      ^                        ^
    在头文件正确的情况下,如果还是有下划线报错,那就应该是缺少模块,需要加上例如:QT += printsupport   

15、 359: error: too few arguments to function 'QTextStream& reset(QTextStream&)'
     reset();
     这个问题就需要看源码里的rest是调用的谁的,为什么出问题。只看这个错误信息是会误导我们的,这个问题实际是
     Qt5 QAbstractItemModel 已经弃用了rest()方法。
     其实在Qt4的rest()方法说明中就已经提到了相关的建议:
     Note: Use beginResetModel() and endResetModel() instead whenever possible. Use this method only if there is no way to call beginResetModel() before invalidating the model. Otherwise it could lead to unexpected behaviour, especially when used with proxy models.
     使用下面两句代替
    QAbstractItemModel::beginResetModel();
    QAbstractItemModel::endResetModel();
15、127: error: reference to 'u16string' is ambiguous
   signed8_t AddUnicodeString(const u16string& newdata, XlsUnicodeStringFormat_t fmt /* = LEN2_FLAGS_UNICODE */ );

u16string同库中的类型同名,混淆出错,更改自己代码里的名字即可

16、4: error: QPlastiqueStyle: No such file or directory     
QPlastiqueStyle已被弃用
我的代码用到的比较简单只是实现了一个虚函数pixelMetric,所以我用QProxyStyle来替换可以编译通过不知道效果怎么样。
参考链接:

http://en.findeen.com/qplastiquestyle_qt5.html 
https://stackoverflow.com/questions/29446642/qt-qplastiquestyle-does-not-name-a-type        
17、485: error: 'QVariant::QVariant(Qt::GlobalColor)' is private
     QVariant(Qt::GlobalColor) Q_DECL_EQ_DELETE;
     解决:QColor(Qt::red) instead of QVariant v = Qt::red
    https://blog.csdn.net/jb11lzy/article/details/48243147
    https://stackoverflow.com/questions/15008297/qvariantqvariantqtglobalcolor-is-private/15008425#15008425
18、11: error: QWindowsStyle: No such file or directory
  Qt5已弃用QWindowsStyle
  窗体风格类 QWindowsStyle 移除了,
使用 QCommonStyle 代替。

19、24: error: ambiguating new declaration of 'int callbackClientEvent(int, int, const char*, int)'
 int __stdcall callbackClientEvent(int TxSocket, int nMsgType, const char *pByteData, int nLen)

解决办法:在声名中同样添加 __stdcall

关于 __stdcall的链接:

https://blog.csdn.net/feixiang_song/article/details/23938745 

http://bbs.chinaunix.net/thread-4116400-1-1.html

关于 Qt4升级Qt5的相关可参考链接:

https://blog.csdn.net/qq61394323/article/details/45692889
http://bbs.qter.org/forum.php?mod=viewthread&tid=903
https://www.cnblogs.com/lzyuse/p/10438958.html

https://blog.csdn.net/qq_25800311/article/details/79451101

发布了27 篇原创文章 · 获赞 25 · 访问量 12万+

猜你喜欢

转载自blog.csdn.net/weixin_38293850/article/details/99694989
今日推荐