Skip to content

Commit 32d0f1e

Browse files
committed
fix: resolve Qt6 compatibility issues
1. Replace deprecated qAsConst with std::as_const for Qt6 compatibility 2. Update key sequence handling to use | operator instead of + for Qt6 3. Fix touch event handling for Qt6 API changes 4. Replace stateChanged with checkStateChanged for Qt6 checkbox signals 5. Add version checks for Qt6-specific API changes 6. Fix various other Qt6 deprecation warnings These changes ensure the codebase works correctly with both Qt5 and Qt6, addressing API changes and deprecations in Qt6 while maintaining backward compatibility. Key technical changes include: - Using Qt version checks for API differences - Standardizing on newer APIs where possible - Maintaining compatibility with both major Qt versions fix: 解决Qt6兼容性问题 1. 使用std::as_const替代已弃用的qAsConst以兼容Qt6 2. 更新键序列处理,在Qt6中使用|运算符而非+ 3. 修复Qt6中触摸事件处理的API变更 4. 将stateChanged替换为checkStateChanged以适配Qt6复选框信号 5. 添加Qt6特定API变更的版本检查 6. 修复其他Qt6弃用警告 这些修改确保代码库能正确工作在Qt5和Qt6环境下,解决了Qt6中的API变更和弃用 问题,同时保持向后兼容性。关键技术变更包括: - 针对API差异使用Qt版本检查 - 尽可能标准化使用新API - 保持对两个主要Qt版本的兼容性
1 parent 3a4600c commit 32d0f1e

23 files changed

+153
-43
lines changed

examples/collections/editexample.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,11 @@ DKeySequenceEditExample::DKeySequenceEditExample(QWidget *parent)
373373
closeLabel1->setFixedSize(72, 19);
374374
closeLabel1->setAlignment(Qt::AlignLeft);
375375
DKeySequenceEdit *closeEdit1 = new DKeySequenceEdit(this);
376+
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
377+
closeEdit1->setKeySequence(QKeySequence(Qt::ALT | Qt::Key_F4));
378+
#else
376379
closeEdit1->setKeySequence(QKeySequence(Qt::ALT + Qt::Key_F4));
380+
#endif
377381
QLabel *closeLabel2 = new QLabel("关闭窗口", this);
378382
closeLabel2->setFixedSize(72, 19);
379383
closeLabel2->setAlignment(Qt::AlignLeft);

examples/collections/mainwindow.cpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -313,18 +313,11 @@ void MainWindow::menuItemInvoked(QAction *action)
313313
_printer->newPage();
314314

315315
// 给出调用方widget界面作为打印内容
316-
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
317-
double xscale = _printer->pageRect().width() / double(this->width());
318-
double yscale = _printer->pageRect().height() / double(this->height());
319-
double scale = qMin(xscale, yscale);
320-
painter.translate(_printer->pageRect().width() / 2.0, _printer->pageRect().height() / 2.0);
321-
#else
322316
double xscale = _printer->pageLayout().paintRectPixels(_printer->resolution()).width() / double(this->width());
323317
double yscale = _printer->pageLayout().paintRectPixels(_printer->resolution()).height() / double(this->height());
324318
double scale = qMin(xscale, yscale);
325319
painter.translate(_printer->pageLayout().paintRectPixels(_printer->resolution()).width() / 2.0,
326320
_printer->pageLayout().paintRectPixels(_printer->resolution()).height() /2.0);
327-
#endif
328321
painter.scale(scale, scale);
329322
painter.translate(-this->width() / 2, -this->height() / 2);
330323
this->render(&painter);
@@ -333,11 +326,7 @@ void MainWindow::menuItemInvoked(QAction *action)
333326
QFont font /*("CESI仿宋-GB2312")*/;
334327
font.setPixelSize(16);
335328
font = QFont(font, painter.device());
336-
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
337-
QRectF rect = _printer->pageRect();
338-
#else
339329
QRectF rect = _printer->pageLayout().paintRectPixels(_printer->resolution());
340-
#endif
341330
rect = QRectF(0, 0, rect.width(), rect.height());
342331
painter.setFont(font);
343332
// 画可用页面矩形,提供调试效果参考

include/widgets/dinputdialog.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,20 +106,20 @@ class DInputDialog : public DDialog
106106

107107
static QString getText(QWidget *parent, const QString &title, const QString &message,
108108
QLineEdit::EchoMode echo = QLineEdit::Normal,
109-
const QString &text = QString(), bool *ok = 0, Qt::WindowFlags flags = Qt::WindowFlags{0},
109+
const QString &text = QString(), bool *ok = 0, Qt::WindowFlags flags = {},
110110
Qt::InputMethodHints inputMethodHints = Qt::ImhNone);
111111

112112
static QString getItem(QWidget *parent, const QString &title, const QString &message,
113113
const QStringList &items, int current = 0, bool editable = true,
114-
bool *ok = 0, Qt::WindowFlags flags = Qt::WindowFlags{0},
114+
bool *ok = 0, Qt::WindowFlags flags = {},
115115
Qt::InputMethodHints inputMethodHints = Qt::ImhNone);
116116

117117
static int getInt(QWidget *parent, const QString &title, const QString &message, int value = 0,
118118
int minValue = -2147483647, int maxValue = 2147483647,
119-
int step = 1, bool *ok = 0, Qt::WindowFlags flags = Qt::WindowFlags{0});
119+
int step = 1, bool *ok = 0, Qt::WindowFlags flags = {});
120120
static double getDouble(QWidget *parent, const QString &title, const QString &message, double value = 0,
121121
double minValue = -2147483647, double maxValue = 2147483647,
122-
int decimals = 1, bool *ok = 0, Qt::WindowFlags flags = Qt::WindowFlags{0});
122+
int decimals = 1, bool *ok = 0, Qt::WindowFlags flags = {});
123123

124124
protected:
125125
void showEvent(QShowEvent *e);

plugin/dtkuidemo/mainwindow.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,11 @@ MainWindow::MainWindow(QWidget *parent) :
128128
// 设置 key seq
129129
DKeySequenceEdit *keyEditer = ui->Dtk__Widget__DKeySequenceEdit;
130130
keyEditer->setAlignment(Qt::AlignCenter);
131+
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
132+
keyEditer->setKeySequence(QKeySequence(Qt::CTRL | Qt::Key_A)); // 用|号,不要用+号
133+
#else
131134
keyEditer->setKeySequence(QKeySequence(Qt::CTRL + Qt::Key_A)); // 用+号不要用逗号
135+
#endif
132136
connect(ui->Dtk__Widget__DKeySequenceEdit, &DKeySequenceEdit::keySequenceChanged, [] (const QKeySequence &seq) {
133137
qDebug() << "Key sequence: " << seq;
134138
});

plugin/dtkuiplugin/dcustomermacrowidget.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@
77

88
#include <QtUiPlugin/QDesignerCustomWidgetInterface>
99

10+
// 定义键位组合操作符,兼容DTK5和DTK6
11+
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
12+
#define DTKEY_OPERATOR |
13+
#else
14+
#define DTKEY_OPERATOR +
15+
#endif
16+
1017
#include <DFrame>
1118
#include <DArrowLineDrawer>
1219
#include <DButtonBox>
@@ -121,7 +128,7 @@ public:
121128
} \
122129
if (IS_SAME(UPPER_NAME, DKeySequenceEdit)) { \
123130
DKeySequenceEdit *w = new DKeySequenceEdit(parent); \
124-
w->setKeySequence(QKeySequence(Qt::CTRL + Qt::Key_A)); /* 用+号不要用逗号 */ \
131+
w->setKeySequence(QKeySequence(Qt::CTRL DTKEY_OPERATOR Qt::Key_A)); /* 用+号不要用逗号 */ \
125132
return reinterpret_cast<UPPER_NAME *>(w); \
126133
} \
127134
if (IS_SAME(UPPER_NAME, DWaterProgress)) { \

src/util/daccessibilitychecker.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ void DAccessibilityCheckerPrivate::checkWidgetName()
9595
for (const QWidget *topLevelWidget : topLevelWidgets)
9696
childrenList.append(topLevelWidget->findChildren<QWidget *>());
9797

98-
for (auto child : qAsConst(childrenList)) {
98+
for (auto child : std::as_const(childrenList)) {
9999
if (q->isIgnore(DAccessibilityChecker::Widget, child)) {
100100
widgetIgnoredCount++;
101101
continue;
@@ -134,7 +134,7 @@ void DAccessibilityCheckerPrivate::checkViewItemName()
134134
for (const QWidget *topLevelWidget : topLevelWidgets)
135135
listViewList.append(topLevelWidget->findChildren<QAbstractItemView *>());
136136

137-
for (auto absListView : qAsConst(listViewList)) {
137+
for (auto absListView : std::as_const(listViewList)) {
138138
if (q->isIgnore(DAccessibilityChecker::ViewItem, absListView))
139139
continue;
140140

src/widgets/daboutdialog.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,11 @@ QPixmap DAboutDialog::companyLogo() const
372372
{
373373
D_DC(DAboutDialog);
374374

375+
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
376+
return d->companyLogoLabel->pixmap(Qt::ReturnByValue);
377+
#else
375378
return d->companyLogoLabel->pixmap();
379+
#endif
376380
}
377381

378382
/*!

src/widgets/dapplication.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ bool DApplicationPrivate::setSingleInstanceBySemaphore(const QString &key)
179179
singleInstance = tryAcquireSystemSemaphore(&ss);
180180

181181
if (singleInstance) {
182-
QtConcurrent::run([this] {
182+
(void)QtConcurrent::run([this] {
183183
QPointer<DApplication> that = q_func();
184184

185185
while (ss.acquire() && singleInstance)

src/widgets/ddialog.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ void DDialog::insertButton(int index, QAbstractButton *button, bool isDefault)
626626

627627
const QString &text = button->text();
628628

629-
if (text.count() == 2) {
629+
if (text.size() == 2) {
630630
for (const QChar &ch : text) {
631631
switch (ch.script()) {
632632
case QChar::Script_Han:

src/widgets/dimageviewer.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -909,18 +909,30 @@ bool DImageViewer::event(QEvent *event)
909909
}
910910
case QEvent::TouchUpdate: {
911911
QTouchEvent *touchEvent = dynamic_cast<QTouchEvent *>(event);
912+
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
913+
QList<QTouchEvent::TouchPoint> touchPoints = touchEvent->points();
914+
#else
912915
QList<QTouchEvent::TouchPoint> touchPoints = touchEvent->touchPoints();
916+
#endif
913917
if (touchPoints.size() > touchCount) {
914918
touchCount = touchPoints.size();
915919
}
916920
break;
917921
}
918922
case QEvent::TouchEnd: {
919923
QTouchEvent *touchEvent = dynamic_cast<QTouchEvent *>(event);
924+
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
925+
QList<QTouchEvent::TouchPoint> touchPoints = touchEvent->points();
926+
#else
920927
QList<QTouchEvent::TouchPoint> touchPoints = touchEvent->touchPoints();
928+
#endif
921929
if (touchPoints.size() == 1 && touchCount <= 1) {
922930
// Swipe gesture.
931+
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
932+
qreal offset = touchPoints.at(0).lastPosition().x() - touchPoints.at(0).pressPosition().x();
933+
#else
923934
qreal offset = touchPoints.at(0).lastPos().x() - touchPoints.at(0).startPos().x();
935+
#endif
924936
if (qAbs(offset) > 200) {
925937
if (offset > 0) {
926938
Q_EMIT requestPreviousImage();

0 commit comments

Comments
 (0)