firemail

标题: 从QT4、QT5到QT6的版本移植总结 [打印本页]

作者: Qter    时间: 2023-2-11 15:46
标题: 从QT4、QT5到QT6的版本移植总结

https://blog.csdn.net/qq_34764580/article/details/119597262?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EOPENSEARCH%7ERate-1-119597262-blog-125746580.pc_relevant_default&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EOPENSEARCH%7ERate-1-119597262-blog-125746580.pc_relevant_default&utm_relevant_index=2



直观总结

经验总结

//pro文件引入模块greaterThan(QT_MAJOR_VERSION, 4): QT += widgetsgreaterThan(QT_MAJOR_VERSION, 5): QT += core5compat//代码中引入头文件#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))#include <QtWidgets>#endif#if (QT_VERSION >= QT_VERSION_CHECK(6,0,0))#include <QtCore5Compat>#endif#if (QT_VERSION >= QT_VERSION_CHECK(6,0,0))QGuiApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::Floor);#endif#if (QT_VERSION >= QT_VERSION_CHECK(6,0,0))std::sort(ipv4s.begin(), ipv4s.end());#elseqSort(ipv4s);#endif#if (QT_VERSION > QT_VERSION_CHECK(5,0,0))#include "qscreen.h"#define deskGeometry qApp->primaryScreen()->geometry()#define deskGeometry2 qApp->primaryScreen()->availableGeometry()#else#include "qdesktopwidget.h"#define deskGeometry qApp->desktop()->geometry()#define deskGeometry2 qApp->desktop()->availableGeometry()#endif//获取当前屏幕索引int QUIHelper::getScreenIndex(){//需要对多个屏幕进行处理int screenIndex = 0;#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))int screenCount = qApp->screens().count();#elseint screenCount = qApp->desktop()->screenCount();#endifif (screenCount > 1) {//找到当前鼠标所在屏幕QPoint pos = QCursor::pos();for (int i = 0; i < screenCount; ++i) {#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))if (qApp->screens().at(i)->geometry().contains(pos)) {#elseif (qApp->desktop()->screenGeometry(i).contains(pos)) {#endifscreenIndex = i;break;}}}return screenIndex;}//获取当前屏幕尺寸区域QRect QUIHelper::getScreenRect(bool available){QRect rect;int screenIndex = QUIHelper::getScreenIndex();if (available) {#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))rect = qApp->screens().at(screenIndex)->availableGeometry();#elserect = qApp->desktop()->availableGeometry(screenIndex);#endif} else {#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))rect = qApp->screens().at(screenIndex)->geometry();#elserect = qApp->desktop()->screenGeometry(screenIndex);#endif}return rect;}//模拟鼠标滚轮#if (QT_VERSION < QT_VERSION_CHECK(6,0,0))QWheelEvent wheelEvent(QPoint(0, 0), -scal, Qt::LeftButton, Qt::NoModifier);#elseQWheelEvent wheelEvent(QPointF(0, 0), QPointF(0, 0), QPoint(0, 0), QPoint(0, -scal), Qt::LeftButton, Qt::NoModifier, Qt::ScrollBegin, false);#endifQApplication::sendEvent(widget, &wheelEvent);//鼠标滚轮直接修改值QWheelEvent *whellEvent = (QWheelEvent *)event;//滚动的角度,*8就是鼠标滚动的距离#if (QT_VERSION < QT_VERSION_CHECK(6,0,0))int degrees = whellEvent->delta() / 8;#elseint degrees = whellEvent->angleDelta().x() / 8;#endif//滚动的步数,*15就是鼠标滚动的角度int steps = degrees / 15;
#if (QT_VERSION >= QT_VERSION_CHECK(6,0,0))bool nativeEvent(const QByteArray &eventType, void *message, qintptr *result);#elsebool nativeEvent(const QByteArray &eventType, void *message, long *result);#endif





欢迎光临 firemail (http://firemail.wang:8088/) Powered by Discuz! X3