Skip to content

Commit 89ff382

Browse files
committed
fix: add Qt6 compatibility and resolve build warnings
1. Added Qt6 version checks for touch/mouse event handling with new API (globalPosition vs screenPos) 2. Marked DNativeSettings, DPlatformSettings and DXcbXSettings classes as Q_DECL_HIDDEN 3. Replaced QGuiApplication::fontChanged signal with event filter for Qt6 compatibility 4. Updated type checking from QVariant::type() to typeId() for Qt6 5. Fixed window geometry calculations for Qt6 mouse position handling 6. Adjusted DHighDpi initialization logic for Qt6 where high DPI is always enabled 7. Added proper private section to DPlatformSettings class fix: 添加Qt6兼容性并解决编译警告 1. 为触摸/鼠标事件处理添加Qt6版本检查,使用新API(globalPosition替 代screenPos) 2. 将DNativeSettings、DPlatformSettings和DXcbXSettings类标记为 Q_DECL_HIDDEN 3. 为Qt6兼容性使用事件过滤器替代QGuiApplication::fontChanged信号 4. 将类型检查从QVariant::type()更新为typeId()以兼容Qt6 5. 修复Qt6鼠标位置处理的窗口几何计算 6. 调整DHighDpi初始化逻辑以适应Qt6中始终启用高DPI的情况 7. 为DPlatformSettings类添加适当的私有部分
1 parent 2f9c1cb commit 89ff382

11 files changed

+98
-9
lines changed

src/ddesktopinputselectioncontrol.cpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -475,9 +475,15 @@ bool DDesktopInputSelectionControl::eventFilter(QObject *object, QEvent *event)
475475
break;
476476

477477
QTouchEvent *touchEvent = static_cast<QTouchEvent *>(event);
478+
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
479+
const auto& tpList = touchEvent->points();
480+
const auto& tp = tpList.first();
481+
QPointF touchPos = tp.lastPosition();
482+
#else
478483
QList<QTouchEvent::TouchPoint> tpList = touchEvent->touchPoints();
479484
QTouchEvent::TouchPoint tp = tpList.first();
480485
QPointF touchPos = tp.lastPos();
486+
#endif
481487

482488
// 有效点击位置
483489
QRectF effectiveRect = anchorRectangle();
@@ -511,7 +517,11 @@ bool DDesktopInputSelectionControl::eventFilter(QObject *object, QEvent *event)
511517
break;
512518

513519
QMouseEvent *me = static_cast<QMouseEvent *>(event);
514-
const QPoint mousePos = me->screenPos().toPoint();
520+
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
521+
QPoint mousePos = me->globalPosition().toPoint();
522+
#else
523+
QPoint mousePos = me->screenPos().toPoint();
524+
#endif
515525

516526
// calculate distances from mouse pos to each handle,
517527
// then choose to interact with the nearest handle
@@ -556,7 +566,12 @@ bool DDesktopInputSelectionControl::eventFilter(QObject *object, QEvent *event)
556566
m_otherSelectionPoint = QPoint(otherRect.x() + otherRect.width() / 2, otherRect.bottom() + 4);
557567
}
558568

559-
QMouseEvent *mouseEvent = new QMouseEvent(me->type(), me->localPos(), me->windowPos(), me->screenPos(),
569+
QMouseEvent *mouseEvent = new QMouseEvent(me->type(),
570+
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
571+
me->position(), me->scenePosition(), me->globalPosition(),
572+
#else
573+
me->localPos(), me->windowPos(), me->screenPos(),
574+
#endif
560575
me->button(), me->buttons(), me->modifiers(), me->source());
561576
m_eventQueue.append(mouseEvent);
562577
return true;
@@ -570,7 +585,11 @@ bool DDesktopInputSelectionControl::eventFilter(QObject *object, QEvent *event)
570585
m_handleVisible = true;
571586

572587
QMouseEvent *me = static_cast<QMouseEvent *>(event);
588+
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
589+
QPoint mousePos = me->globalPosition().toPoint();
590+
#else
573591
QPoint mousePos = me->screenPos().toPoint();
592+
#endif
574593
if (m_handleState == HandleIsHeld) {
575594
QPoint delta = m_handleDragStartedPosition - mousePos;
576595
const int startDragDistance = QGuiApplication::styleHints()->startDragDistance();

src/dnativesettings.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
DPP_BEGIN_NAMESPACE
1818

1919
class DPlatformSettings;
20-
class DNativeSettings : public QAbstractDynamicMetaObject
20+
class Q_DECL_HIDDEN DNativeSettings : public QAbstractDynamicMetaObject
2121
{
2222
public:
2323
explicit DNativeSettings(QObject *base, DPlatformSettings *settings, bool global_settings);

src/dplatformsettings.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ QT_END_NAMESPACE
1515

1616
DPP_BEGIN_NAMESPACE
1717

18-
class DPlatformSettings
18+
class Q_DECL_HIDDEN DPlatformSettings
1919
{
2020
public:
2121
virtual ~DPlatformSettings() {}
@@ -41,6 +41,7 @@ class DPlatformSettings
4141
void handlePropertyChanged(const QByteArray &property, const QVariant &value);
4242
void handleNotify(const QByteArray &signal, qint32 data1, qint32 data2);
4343

44+
private:
4445
struct Q_DECL_HIDDEN Callback
4546
{
4647
PropertyChangeFunc func;

src/dselectedtexttooltip.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,12 @@ DSelectedTextTooltip::DSelectedTextTooltip()
3636
m_textInfoVec.push_back({Paste, 0, qApp->translate("QLineEdit", "&Paste").split("(").at(0)});
3737

3838
updateColor();
39+
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
40+
// Qt6中使用事件处理替代fontChanged信号
41+
qApp->installEventFilter(this);
42+
#else
3943
connect(qApp, &QGuiApplication::fontChanged, this, &DSelectedTextTooltip::onFontChanged);
44+
#endif
4045

4146
// 更新文本信息
4247
onFontChanged();
@@ -46,6 +51,16 @@ DSelectedTextTooltip::~DSelectedTextTooltip()
4651
{
4752
}
4853

54+
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
55+
bool DSelectedTextTooltip::eventFilter(QObject *obj, QEvent *event)
56+
{
57+
if (event->type() == QEvent::ApplicationFontChange) {
58+
onFontChanged();
59+
}
60+
return QRasterWindow::eventFilter(obj, event);
61+
}
62+
#endif
63+
4964
void DSelectedTextTooltip::onFontChanged()
5065
{
5166
QFontMetrics font_metrics(qApp->font());

src/dselectedtexttooltip.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ class DSelectedTextTooltip : public QRasterWindow
3131
protected:
3232
void paintEvent(QPaintEvent *pe) override;
3333
void mousePressEvent(QMouseEvent *event) override;
34+
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
35+
bool eventFilter(QObject *obj, QEvent *event) override;
36+
#endif
3437

3538
private slots:
3639
void onFontChanged();

src/dxcbxsettings.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,11 @@ class Q_DECL_HIDDEN DXcbXSettingsPrivate
405405
const QByteArray &key = i.key();
406406
quint16 key_size = key.size();
407407

408+
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
409+
switch (value.value.typeId()) {
410+
#else
408411
switch (value.value.type()) {
412+
#endif
409413
case QMetaType::QColor:
410414
type = XSettingsTypeColor;
411415
break;

src/dxcbxsettings.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ DPP_BEGIN_NAMESPACE
2121

2222
class DXcbXSettingsPrivate;
2323

24-
class DXcbXSettings : public DPlatformSettings
24+
class Q_DECL_HIDDEN DXcbXSettings : public DPlatformSettings
2525
{
2626
Q_DECLARE_PRIVATE(DXcbXSettings)
2727
public:

xcb/dframewindow.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,23 +515,41 @@ void DFrameWindow::mouseMoveEvent(QMouseEvent *event)
515515
}
516516
set_edge:
517517
/// begin set cursor edge type
518+
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
519+
if (event->position().x() <= m_contentGeometry.x()) {
520+
#else
518521
if (event->x() <= m_contentGeometry.x()) {
522+
#endif
519523
if (isFixedWidth)
520524
goto skip_set_cursor;
521525

522526
mouseCorner = Utility::LeftEdge;
527+
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
528+
} else if (event->position().x() < m_contentGeometry.right()) {
529+
#else
523530
} else if (event->x() < m_contentGeometry.right()) {
531+
#endif
524532
if (isFixedHeight)
525533
goto skip_set_cursor;
526534

535+
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
536+
if (event->position().y() <= m_contentGeometry.y()) {
537+
mouseCorner = Utility::TopEdge;
538+
} else if (!isFixedWidth || event->position().y() >= m_contentGeometry.bottom()) {
539+
#else
527540
if (event->y() <= m_contentGeometry.y()) {
528541
mouseCorner = Utility::TopEdge;
529542
} else if (!isFixedWidth || event->y() >= m_contentGeometry.bottom()) {
543+
#endif
530544
mouseCorner = Utility::BottomEdge;
531545
} else {
532546
goto skip_set_cursor;
533547
}
548+
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
549+
} else if (!isFixedWidth && (!isFixedHeight || event->position().x() >= m_contentGeometry.right())) {
550+
#else
534551
} else if (!isFixedWidth && (!isFixedHeight || event->x() >= m_contentGeometry.right())) {
552+
#endif
535553
mouseCorner = Utility::RightEdge;
536554
} else {
537555
goto skip_set_cursor;

xcb/dhighdpi.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,13 @@ inline static void init()
3939
Q_CONSTRUCTOR_FUNCTION(init)
4040
void DHighDpi::init()
4141
{
42-
if (QGuiApplication::testAttribute(Qt::AA_DisableHighDpiScaling)
43-
// 可以禁用此行为
44-
|| qEnvironmentVariableIsSet("D_DXCB_DISABLE_OVERRIDE_HIDPI")
42+
if (qEnvironmentVariableIsSet("D_DXCB_DISABLE_OVERRIDE_HIDPI")
4543
// 无有效的xsettings时禁用
46-
|| !DXcbXSettings::getOwner()) {
44+
|| !DXcbXSettings::getOwner()
45+
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
46+
|| QGuiApplication::testAttribute(Qt::AA_DisableHighDpiScaling)
47+
#endif
48+
) {
4749
// init函数可能会被重复调用, 此处应该清理VtableHook
4850
if (active) {
4951
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
@@ -67,10 +69,13 @@ void DHighDpi::init()
6769
qunsetenv("QT_USE_PHYSICAL_DPI");
6870
}
6971

72+
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
73+
// Qt6中高DPI缩放总是启用的,无需设置AA_EnableHighDpiScaling
7074
if (!QGuiApplication::testAttribute(Qt::AA_EnableHighDpiScaling)) {
7175
QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
7276
QHighDpiScaling::initHighDpiScaling();
7377
}
78+
#endif
7479

7580
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
7681
active = VtableHook::overrideVfptrFun(&QXcbScreen::pixelDensity, pixelDensity);

xcb/dnotitlebarwindowhelper.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,11 +559,19 @@ bool DNoTitlebarWindowHelper::windowEvent(QEvent *event)
559559
#endif
560560
}
561561
if (isTouchDown && event->type() == QEvent::MouseButtonPress) {
562+
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
563+
touchBeginPosition = static_cast<QMouseEvent*>(event)->globalPosition().toPoint();
564+
#else
562565
touchBeginPosition = static_cast<QMouseEvent*>(event)->globalPos();
566+
#endif
563567
}
564568
// add some redundancy to distinguish trigger between system menu and system move
565569
if (event->type() == QEvent::MouseMove) {
570+
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
571+
QPointF currentPos = static_cast<QMouseEvent*>(event)->globalPosition();
572+
#else
566573
QPointF currentPos = static_cast<QMouseEvent*>(event)->globalPos();
574+
#endif
567575
QPointF delta = touchBeginPosition - currentPos;
568576
if (delta.manhattanLength() < QGuiApplication::styleHints()->startDragDistance()) {
569577
return VtableHook::callOriginalFun(w, &QWindow::event, event);
@@ -590,7 +598,11 @@ bool DNoTitlebarWindowHelper::windowEvent(QEvent *event)
590598
// keeping the moving state, we can just reset ti back to normal.
591599
if (event->type() == QEvent::MouseButtonPress) {
592600
self->m_windowMoving = false;
601+
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
602+
g_pressPoint[this] = dynamic_cast<QMouseEvent*>(event)->globalPosition().toPoint();
603+
#else
593604
g_pressPoint[this] = dynamic_cast<QMouseEvent*>(event)->globalPos();
605+
#endif
594606
}
595607

596608
// ========== 修改:鼠标事件处理保持原有逻辑 ==========
@@ -605,7 +617,11 @@ bool DNoTitlebarWindowHelper::windowEvent(QEvent *event)
605617
return ret;
606618
}
607619

620+
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
621+
QPointF delta = me->globalPosition() - g_pressPoint[this];
622+
#else
608623
QPointF delta = me->globalPos() - g_pressPoint[this];
624+
#endif
609625
if (delta.manhattanLength() < QGuiApplication::styleHints()->startDragDistance()) {
610626
return ret;
611627
}

0 commit comments

Comments
 (0)