记录QT5迁移到QT6.8上的一些问题
经常看到有的同学说网上的教程都是假的,巴拉巴拉,看看人家发布时间,Qt官方的API都会有所变动,多搜索,多总结,再修改记录。
下次遇到问题多这样搜索 QT 4/5/6 xxx document,对比一下就知道变动了
1.'endl' was not declared in this scope
qDebug()<<data.toHex()<<endl;
endl改成Qt::endl
qDebug()<<data.toHex()<<Qt::endl;
2.Qt6移除了<QDesktopWidget>的问题
..\..\mainwidget.h:6:10: fatal error: QDesktopWidget: No such file or directory
修改前:
//头文件
#include <QDesktopWidget>
//CPP文件
QDesktopWidget* desktopWidget = QApplication::desktop();
QRect screenRect = desktopWidget->screenGeometry();
currentScreenWid = (screenRect.width());
currentScreenHei = (screenRect.height() - 100);
this->setFixedSize(currentScreenWid,currentScreenHei);
改成:
//头文件
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
#include <QScreen>
#else
#include <QDesktopWidget>
#endif
//CPP文件
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
QScreen *pScreen = QApplication::primaryScreen();
currentScreenWid = (pScreen->geometry().width());
currentScreenHei = (pScreen->geometry().height() - 100);
#else
QDesktopWidget* desktopWidget = QApplication::desktop();
QRect screenRect = desktopWidget->screenGeometry();
// currentScreenWid = (screenRect.width()*3/4);
// currentScreenHei = (screenRect.height()*4/5 - 100);
currentScreenWid = (screenRect.width());
currentScreenHei = (screenRect.height() - 100);
#endif
this->setFixedSize(currentScreenWid,currentScreenHei);
3.error: 'class QGridLayout' has no member named 'setMargin'
使用setContentsMargins替代
gridlayout_left->setContentsMargins(0,25,0,20);
gridlayout_right->setContentsMargins(0,25,0,20);
4.调用的库是32位的
QT6下载的编译器都是64位的,把32位库文件换成64位。
OK,运行了
5.部分信号与槽没反应,估计是Qt4格式的,过时了
qt.core.qobject.connect: QObject::connect: No such signal QButtonGroup::buttonClicked(int) in ..\..\menubarwid.cpp:82
connect(pushButtonGroup, SIGNAL(buttonClicked(int)), this, SLOT(slot_btnGroupClicked(int)));
好家伙居然还是用的Qt4的信号
Qt5就已经过时了,但是调用依然有效
Qt6彻底删了
修改位idClicked(int)后正常:
connect(pushButtonGroup, SIGNAL(idClicked(int)), this, SLOT(slot_btnGroupClicked(int)));
后面有啥问题再记录吧