Skip to content

Commit df92c6a

Browse files
add-uosdeepin-bot[bot]
authored andcommitted
refactor(qt): add automatic Qt version detection and update compatibility code
Replace hardcoded Qt version with automatic detection that supports both Qt5 and Qt6. Update all Qt version-specific code to use unified APIs, replacing QRegExp with QRegularExpression and removing version conditionals throughout the codebase.
1 parent 37fd916 commit df92c6a

File tree

11 files changed

+52
-169
lines changed

11 files changed

+52
-169
lines changed

CMakeLists.txt

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,20 @@ if(CMAKE_BUILD_TYPE STREQUAL "Debug")
1616
add_definitions(-DDISABLE_POLKIT)
1717
endif()
1818

19-
# 设置 QT_VERSION 变量
20-
set(QT_VERSION_MAJOR 6)
19+
# 自动检测系统中的Qt版本
20+
if(NOT DEFINED QT_VERSION_MAJOR)
21+
# 首先尝试查找Qt6
22+
find_package(Qt6 QUIET)
23+
if(Qt6_FOUND)
24+
set(QT_VERSION_MAJOR 6)
25+
message("Found Qt6, using Qt version 6")
26+
else()
27+
set(QT_VERSION_MAJOR 5)
28+
message("Found Qt5, using Qt version 5")
29+
endif()
30+
else()
31+
message("Using manually specified QT_VERSION_MAJOR: ${QT_VERSION_MAJOR}")
32+
endif()
2133

2234
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wl,-z,relro -Wl,-z,now")
2335

@@ -28,4 +40,4 @@ add_subdirectory(${CMAKE_SOURCE_DIR}/deepin-devicemanager-server)
2840
if(CMAKE_COVERAGE_ARG STREQUAL "CMAKE_COVERAGE_ARG_ON")
2941
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -Wall -fprofile-arcs -ftest-coverage")
3042
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -Wall -fprofile-arcs -ftest-coverage")
31-
endif()
43+
endif()

deepin-devicemanager/CMakeLists.txt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,19 +145,18 @@ SET_QT_VERSION()
145145

146146
if(${QT_VERSION_MAJOR} EQUAL 6)
147147
find_package(QApt-qt6 REQUIRED)
148-
include_directories(${QApt-qt6_INCLUDE_DIRS})
148+
include_directories(${QApt-qt6_INCLUDE_DIRS})
149+
include_directories(${Qt6Gui_PRIVATE_INCLUDE_DIRS})
149150
set(QAPT_LIB QApt-qt6)
150151
elseif(${QT_VERSION_MAJOR} EQUAL 5)
151152
find_package(QApt REQUIRED)
152153
include_directories(${QApt_INCLUDE_DIRS})
154+
include_directories(${Qt5Gui_PRIVATE_INCLUDE_DIRS})
153155
set(QAPT_LIB QApt)
154156
else()
155157
message(FATAL_ERROR "Unsupported QT_VERSION_MAJOR: ${QT_VERSION_MAJOR}")
156158
endif()
157159

158-
#include_directories(${Qt5Gui_PRIVATE_INCLUDE_DIRS})
159-
include_directories(${Qt6Gui_PRIVATE_INCLUDE_DIRS})
160-
161160
# Tell CMake to create the executable
162161
add_executable(${PROJECT_NAME} ${SRC_CPP} ${SRC_H} ${APP_QRC})
163162
target_include_directories(${APP_BIN_NAME} PUBLIC ${DtkWidget_INCLUDE_DIRS} ${OBJECT_BINARY_DIR})
@@ -222,4 +221,4 @@ endif()
222221
# Test--------deepin-devicemanager
223222
if (CMAKE_COVERAGE_ARG STREQUAL "CMAKE_COVERAGE_ARG_ON")
224223
add_subdirectory(./tests)
225-
endif()
224+
endif()

deepin-devicemanager/src/DeviceManager/DeviceInfo.cpp

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -817,11 +817,7 @@ const QString DeviceBaseInfo::getDriverVersion()
817817
}
818818

819819
foreach (QString out, outInfo.split("\n")) {
820-
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
821-
QStringList item = out.split(":", QString::SkipEmptyParts);
822-
#else
823-
QStringList item = out.split(":", Qt::SkipEmptyParts);
824-
#endif
820+
QStringList item = out.split(":", QT_SKIP_EMPTY_PARTS);
825821
if (!item.isEmpty() && "version" == item[0].trimmed()) {
826822
// qCDebug(appLog) << "Found driver version: " << item[1].trimmed();
827823
return item[1].trimmed();
@@ -1077,7 +1073,6 @@ void DeviceBaseInfo::setHwinfoLshwKey(const QMap<QString, QString> &mapInfo)
10771073
<< "o" << "p" << "q" << "r" << "s" << "t"
10781074
<< "u" << "v" << "w" << "x" << "y" << "z";
10791075

1080-
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
10811076
QRegularExpression reg("([0-9a-zA-Z]+)-([0-9a-zA-Z]+)\\.([0-9a-zA-Z]+)");
10821077
QRegularExpressionMatch match = reg.match(words[0]);
10831078
if (match.hasMatch()) {
@@ -1086,27 +1081,12 @@ void DeviceBaseInfo::setHwinfoLshwKey(const QMap<QString, QString> &mapInfo)
10861081
int third = match.captured(3).toInt();
10871082
m_HwinfoToLshw = QString("usb@%1:%2.%3").arg(nums.at(first)).arg(nums.at(second)).arg(nums.at(third));
10881083
qCDebug(appLog) << "DeviceBaseInfo::setHwinfoLshwKey match";
1089-
} else {
1090-
int first = chs[0].toInt();
1091-
int second = chs[1].toInt();
1092-
m_HwinfoToLshw = QString("usb@%1:%2").arg(nums.at(first)).arg(nums.at(second));
1093-
qCDebug(appLog) << "DeviceBaseInfo::setHwinfoLshwKey not match";
1094-
}
1095-
#else
1096-
QRegExp reg("([0-9a-zA-Z]+)-([0-9a-zA-Z]+)\\.([0-9a-zA-Z]+)");
1097-
if (reg.exactMatch(words[0])) {
1098-
int first = reg.cap(1).toInt();
1099-
int second = reg.cap(2).toInt();
1100-
int third = reg.cap(3).toInt();
1101-
m_HwinfoToLshw = QString("usb@%1:%2.%3").arg(nums.at(first)).arg(nums.at(second)).arg(nums.at(third));
1102-
qCDebug(appLog) << "DeviceBaseInfo::setHwinfoLshwKey match";
11031084
} else {
11041085
int first = chs[0].toInt();
11051086
int second = chs[1].toInt();
11061087
m_HwinfoToLshw = QString("usb@%1:%2").arg(nums.at(first)).arg(nums.at(second));
11071088
qCDebug(appLog) << "DeviceBaseInfo::setHwinfoLshwKey no match";
11081089
}
1109-
#endif
11101090
}
11111091

11121092
bool DeviceBaseInfo::matchToLshw(const QMap<QString, QString> &mapInfo)

deepin-devicemanager/src/DeviceManager/DeviceInput.cpp

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -233,35 +233,17 @@ bool DeviceInput::getPS2Syspath(const QString &dfs)
233233
// qCDebug(appLog) << "Found sysfs line: " << sysfs;
234234
continue;
235235
}
236-
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
237-
QRegExp reg("H: Handlers=.*(event[0-9]{1,2}).*");
238-
if (reg.exactMatch(line)) {
239-
event = reg.cap(1);
240-
// qCDebug(appLog) << "Matched event using QRegExp: " << event;
241-
}
242-
#else
243236
QRegularExpression reg("H: Handlers=.*(event[0-9]{1,2}).*");
244237
QRegularExpressionMatch match = reg.match(line);
245238
if (match.hasMatch()) {
246239
event = match.captured(1);
247240
// qCDebug(appLog) << "Matched event using QRegularExpression: " << event;
248241
}
249-
#endif
250242
}
251243

252244
if (!event.isEmpty() && !sysfs.isEmpty()) {
253245
// qCDebug(appLog) << "Event and sysfs are not empty. Checking for match.";
254246
if (event == eventdfs) {
255-
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
256-
QRegExp regfs;
257-
if (sysfs.contains("i2c_designware"))
258-
regfs = QRegExp("S: Sysfs=(.*)/input/input[0-9]{1,2}");
259-
else
260-
regfs = QRegExp("S: Sysfs=(.*)/input[0-9]{1,2}");
261-
if (regfs.exactMatch(sysfs)) {
262-
m_SysPath = regfs.cap(1);
263-
}
264-
#else
265247
QRegularExpression regfs;
266248
if (sysfs.contains("i2c_designware"))
267249
regfs = QRegularExpression("S: Sysfs=(.*)/input/input[0-9]{1,2}");
@@ -271,7 +253,6 @@ bool DeviceInput::getPS2Syspath(const QString &dfs)
271253
if (match.hasMatch()) {
272254
m_SysPath = match.captured(1);
273255
}
274-
#endif
275256
}
276257
}
277258
}
@@ -307,31 +288,18 @@ bool DeviceInput::isBluetoothDevice(const QString &dfs)
307288
Uniq = line;
308289
continue;
309290
}
310-
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
311-
QRegExp reg("H: Handlers=.*(event[0-9]{1,2}).*");
312-
if (reg.exactMatch(line)) {
313-
event = reg.cap(1);
314-
}
315-
#else
316291
QRegularExpression reg("H: Handlers=.*(event[0-9]{1,2}).*");
317292
QRegularExpressionMatch match = reg.match(line);
318293
if (match.hasMatch()) {
319294
event = match.captured(1);
320295
}
321-
#endif
322296
}
323297

324298
if (event == eventdfs) {
325-
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
326-
QRegExp regUniq(".*([0-9A-Z]{2}:[0-9A-Z]{2}:[0-9A-Z]{2}:[0-9A-Z]{2}:[0-9A-Z]{2}:[0-9A-Z]{2}).*");
327-
if (regUniq.exactMatch(Uniq)) {
328-
QString id = regUniq.cap(1);
329-
#else
330299
QRegularExpression regUniq(".*([0-9A-Z]{2}:[0-9A-Z]{2}:[0-9A-Z]{2}:[0-9A-Z]{2}:[0-9A-Z]{2}:[0-9A-Z]{2}).*");
331300
QRegularExpressionMatch match = regUniq.match(Uniq);
332301
if (match.hasMatch()) {
333302
QString id = match.captured(1);
334-
#endif
335303
QProcess process;
336304
process.start("hcitool con");
337305
process.waitForFinished(-1);
@@ -352,16 +320,10 @@ bool DeviceInput::isBluetoothDevice(const QString &dfs)
352320
QString DeviceInput::eventStrFromDeviceFiles(const QString &dfs)
353321
{
354322
qCDebug(appLog) << "eventStrFromDeviceFiles";
355-
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
356323
QRegularExpression regdfs(".*(event[0-9]{1,2}).*");
357324
QRegularExpressionMatch match = regdfs.match(dfs);
358325
if (match.hasMatch())
359326
return match.captured(1);
360-
#else
361-
QRegExp regdfs(".*(event[0-9]{1,2}).*");
362-
if (regdfs.exactMatch(dfs))
363-
return regdfs.cap(1);
364-
#endif
365327
qCDebug(appLog) << "eventStrFromDeviceFiles end";
366328
return "";
367329
}

deepin-devicemanager/src/DeviceManager/DeviceManager.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1364,11 +1364,7 @@ DeviceBaseInfo *DeviceManager::getAudioDevice(const QString &path)
13641364
DeviceAudio *audio = dynamic_cast<DeviceAudio *>(*it);
13651365
QString tpath = audio->uniqueID();
13661366
// 判断该设备是否已经存在,1.1:1.1 -> 1.1:1.0
1367-
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
1368-
if (audio && path == tpath.replace(QRegExp("[1-9]$"), "0")) {
1369-
#else
13701367
if (audio && path == tpath.replace(QRegularExpression("[1-9]$"), "0")) {
1371-
#endif
13721368
return *it;
13731369
}
13741370
QString syspath = audio->sysPath(); //remove dumplicate syspath

deepin-devicemanager/src/DeviceManager/DeviceMonitor.cpp

Lines changed: 7 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -56,48 +56,16 @@ QString DeviceMonitor::parseMonitorSize(const QString &sizeDescription, double &
5656
// 根据不同的正则表达式解析屏幕大小字符串
5757
QString res = sizeDescription;
5858

59-
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
60-
QRegExp re("^([\\d]*)x([\\d]*) mm$");
61-
if (re.exactMatch(sizeDescription)) {
62-
qCDebug(appLog) << "Matched size description with format: ^([\\d]*)x([\\d]*) mm$";
63-
// 获取屏幕宽高 int
64-
m_Width = re.cap(1).toInt();
65-
m_Height = re.cap(2).toInt();
66-
retSize = QSize(m_Width, m_Height);
67-
68-
// 获取屏幕尺寸大小 inch
69-
double width = m_Width / 2.54;
70-
double height = m_Height / 2.54;
71-
inch = std::sqrt(width * width + height * height) / 10.0;
72-
res = QString::number(inch, 10, 1) + " " + translateStr("inch") + " (";
73-
res += sizeDescription;
74-
res += ")";
75-
}
76-
77-
re.setPattern("([0-9]\\d*)mm x ([0-9]\\d*)mm");
78-
if (re.exactMatch(sizeDescription)) {
79-
qCDebug(appLog) << "Matched size description with format: ([0-9]\\d*)mm x ([0-9]\\d*)mm";
80-
// 获取屏幕宽高 int
81-
m_Width = re.cap(1).toInt();
82-
m_Height = re.cap(2).toInt();
83-
retSize = QSize(m_Width, m_Height);
84-
85-
double width = m_Width / 2.54;
86-
double height = m_Height / 2.54;
87-
inch = std::sqrt(width * width + height * height) / 10.0;
88-
res = QString::number(inch, 10, 1) + " " + translateStr("inch") + " (";
89-
res += sizeDescription;
90-
res += ")";
91-
}
92-
#else
9359
QRegularExpression re("^([\\d]*)x([\\d]*) mm$");
9460
QRegularExpressionMatch match = re.match(sizeDescription);
9561
if (match.hasMatch()) {
9662
qCDebug(appLog) << "Matched size description with format: ^([\\d]*)x([\\d]*) mm$";
63+
// 获取屏幕宽高 int
9764
m_Width = match.captured(1).toInt();
9865
m_Height = match.captured(2).toInt();
9966
retSize = QSize(m_Width, m_Height);
10067

68+
// 获取屏幕尺寸大小 inch
10169
double width = m_Width / 2.54;
10270
double height = m_Height / 2.54;
10371
inch = std::sqrt(width * width + height * height) / 10.0;
@@ -110,6 +78,7 @@ QString DeviceMonitor::parseMonitorSize(const QString &sizeDescription, double &
11078
match = re.match(sizeDescription);
11179
if (match.hasMatch()) {
11280
qCDebug(appLog) << "Matched size description with format: ([0-9]\\d*)mm x ([0-9]\\d*)mm";
81+
// 获取屏幕宽高 int
11382
m_Width = match.captured(1).toInt();
11483
m_Height = match.captured(2).toInt();
11584
retSize = QSize(m_Width, m_Height);
@@ -121,7 +90,6 @@ QString DeviceMonitor::parseMonitorSize(const QString &sizeDescription, double &
12190
res += sizeDescription;
12291
res += ")";
12392
}
124-
#endif
12593

12694
qCDebug(appLog) << "Finished parsing monitor size. Result:" << res;
12795
return res;
@@ -164,11 +132,7 @@ void DeviceMonitor::setInfoFromHwinfo(const QMap<QString, QString> &mapInfo)
164132
caculateScreenRatio();
165133

166134
if (Common::isHwPlatform()){
167-
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
168-
m_SupportResolution.replace(QRegExp(", $"), "");
169-
#else
170135
m_SupportResolution.replace(QRegularExpression(", $"), "");
171-
#endif
172136
}
173137
qCDebug(appLog) << "Supported resolutions processed:" << m_SupportResolution;
174138

@@ -288,7 +252,7 @@ bool DeviceMonitor::setInfoFromXradr(const QString &main, const QString &edid, c
288252
// 设置当前分辨率
289253
if (m_CurrentResolution.isEmpty()) {
290254
qCDebug(appLog) << "Current resolution is empty, extracting from main string";
291-
QRegularExpression reScreenSize(".*([0-9]{1,5}x[0-9]{1,5}).*");
255+
QRegularExpression reScreenSize(".*connected.*\\s([0-9]{1,5}x[0-9]{1,5})\\+.*");
292256
QRegularExpressionMatch match = reScreenSize.match(main);
293257
if (match.hasMatch()) {
294258
qCDebug(appLog) << "Matched screen size from main string:" << match.captured(1);
@@ -320,11 +284,7 @@ bool DeviceMonitor::setInfoFromXradr(const QString &main, const QString &edid, c
320284
m_SupportResolution.append(", ");
321285
}
322286
}
323-
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
324-
m_SupportResolution.remove(QRegExp(", $"));
325-
#else
326287
m_SupportResolution.remove(QRegularExpression(", $"));
327-
#endif
328288
}
329289
}
330290
qCDebug(appLog) << "Interface already processed, returning false";
@@ -494,7 +454,7 @@ bool DeviceMonitor::setMainInfoFromXrandr(const QString &info, const QString &ra
494454
}
495455

496456
// 设置当前分辨率
497-
QRegularExpression reScreenSize(".*([0-9]{1,5}x[0-9]{1,5}).*");
457+
QRegularExpression reScreenSize(".*connected.*\\s([0-9]{1,5}x[0-9]{1,5})\\+.*");
498458
match = reScreenSize.match(info);
499459
if (match.hasMatch()) {
500460
qCDebug(appLog) << "Found screen size in xrandr info:" << match.captured(1);
@@ -516,9 +476,9 @@ bool DeviceMonitor::setMainInfoFromXrandr(const QString &info, const QString &ra
516476
m_RefreshRate = QString("%1").arg(curRate);
517477
}
518478
if (Common::specialComType == 5 || Common::specialComType == 6) {
519-
m_CurrentResolution = QString("%1").arg(QT_REGEXP_CAPTURE(reScreenSize, 1, info)).replace("x", "×", Qt::CaseInsensitive);
479+
m_CurrentResolution = QString("%1").arg(match.captured(1)).replace("x", "×", Qt::CaseInsensitive);
520480
} else {
521-
m_CurrentResolution = QString("%1 @%2").arg(QT_REGEXP_CAPTURE(reScreenSize, 1, info)).arg(curRate).replace("x", "×", Qt::CaseInsensitive);
481+
m_CurrentResolution = QString("%1 @%2").arg(match.captured(1)).arg(curRate).replace("x", "×", Qt::CaseInsensitive);
522482
}
523483
} else {
524484
qCDebug(appLog) << "Rate is empty, setting current resolution without rate";

0 commit comments

Comments
 (0)