Skip to content

Commit cdac8dc

Browse files
authored
Merge pull request #4 from KnCRJVirX/feature-qt-version-upgrade
迁移Qt版本到Qt6
2 parents 7df4b53 + d81c1db commit cdac8dc

File tree

8 files changed

+41
-32
lines changed

8 files changed

+41
-32
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,7 @@ CMakeLists.txt.user*
4747
.vs
4848
.vscode
4949
*.code-workspace
50-
*.zip
50+
*.zip
51+
52+
# Build
53+
build/

dogcom.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ void DogCom::FillConfig(QString a, QString p, QString m)
2727
mac_addr = m;
2828
}
2929

30-
void DogCom::print_packet(const char msg[], const unsigned char *packet, int length)
30+
void DogCom::print_packet(const char* msg, const unsigned char *packet, int length)
3131
{
3232
if (!log)
3333
return;
@@ -45,7 +45,7 @@ void DogCom::run()
4545
// qDebug()<<"account:"<<account;
4646
// qDebug()<<"password:"<<password;
4747
// qDebug()<<"mac_addr:"<<mac_addr;
48-
qDebug() << endl;
48+
qDebug() << Qt::endl;
4949
qDebug() << "Start dogcoming...";
5050
// 后台登录维持连接的线程
5151
srand((unsigned int)time(nullptr));
@@ -54,7 +54,7 @@ void DogCom::run()
5454
{
5555
skt.init();
5656
}
57-
catch (DogcomSocketException e)
57+
catch (DogcomSocketException& e)
5858
{
5959
qCritical() << "dogcom socket init error"
6060
<< " msg: " << e.what();

main.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ void LogMsgOutput(QtMsgType type,
5050
QFile file(dir.path() + QString("/logs/log%1.lgt").arg(timePoint));
5151
file.open(QIODevice::WriteOnly | QIODevice::Append);
5252
QTextStream out(&file);
53-
out << log << endl;
53+
out << log << Qt::endl;
5454
file.close();
5555

5656
// 释放锁
@@ -72,7 +72,7 @@ int main(int argc, char *argv[])
7272
#endif
7373

7474
SingleApplication::setQuitOnLastWindowClosed(false);
75-
SingleApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
75+
// SingleApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
7676

7777
qDebug() << "...main...";
7878

@@ -82,7 +82,9 @@ int main(int argc, char *argv[])
8282
a.setFont(font);
8383

8484
QTranslator translator;
85-
translator.load(":/ts/DrCOM_zh_CN.qm");
85+
if (!translator.load(":/ts/DrCOM_zh_CN.qm")) {
86+
qWarning() << "Failed to load translation file!";
87+
}
8688
a.installTranslator(&translator);
8789

8890
MainWindow w(&a);

mainwindow.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <QDebug>
77
#include <QMessageBox>
88
#include <QValidator>
9-
#include <QRegExp>
9+
#include <QRegularExpression>
1010
#include <QSettings>
1111
#include <QWindow>
1212
#include "constants.h"
@@ -15,6 +15,7 @@
1515
#include <QUrl>
1616
#include <QCloseEvent>
1717
#include <QProcess>
18+
#include <QTimer>
1819

1920
MainWindow::MainWindow(SingleApplication *parentApp, QWidget *parent) :
2021
QDialog(parent),
@@ -94,7 +95,7 @@ MainWindow::MainWindow(SingleApplication *parentApp, QWidget *parent) :
9495
this, &MainWindow::HandleIpAddress);
9596

9697
// 验证手动输入的mac地址
97-
macValidator = new QRegExpValidator(QRegExp("[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){5}"));
98+
macValidator = new QRegularExpressionValidator(QRegularExpression("[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){5}"), this);
9899
ui->lineEditMAC->setValidator(macValidator);
99100

100101
// 尚未登录 不可注销
@@ -151,15 +152,15 @@ void MainWindow::RestartDrcom()
151152
QProcess::startDetached(qApp->arguments()[0], qApp->arguments());
152153
qDebug() << "Restart done.";
153154
}
154-
else if(CURR_STATE==STATE_LOGGING)
155-
;// 正在登录时候退出,假装没看到,不理
155+
// else if(CURR_STATE==STATE_LOGGING)
156+
// 正在登录时候退出,假装没看到,不理
156157
}
157158

158159
void MainWindow::QuitDrcom()
159160
{
160161
// 退出之前恢复重试计数
161-
QSettings s(SETTINGS_FILE_NAME);
162-
s.setValue(ID_RESTART_TIMES, 0);
162+
QSettings s(SETTINGS_FILE_NAME, QSettings::IniFormat);
163+
s.setValue(ID_RESTART_TIMES, 0);
163164
qDebug() << "reset restartTimes";
164165
qDebug() << "QuitDrcom";
165166

@@ -168,9 +169,10 @@ void MainWindow::QuitDrcom()
168169
if(CURR_STATE==STATE_ONLINE)
169170
dogcomController->LogOut();
170171
else if(CURR_STATE==STATE_OFFLINE)
171-
qApp->quit();
172-
else if(CURR_STATE==STATE_LOGGING)
173-
;// 正在登录时候退出,假装没看到,不理
172+
// qApp->quit();
173+
QTimer::singleShot(0, qApp, SLOT(quit()));
174+
// else if(CURR_STATE==STATE_LOGGING)
175+
// 正在登录时候退出,假装没看到,不理
174176

175177
// qApp->quit()调用放到了注销响应那块
176178
}

mainwindow.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
#include <QDialog>
55
#include <QSettings>
6-
#include <QRegExpValidator>
6+
#include <QRegularExpressionValidator>
77
#include <dogcomcontroller.h>
88
#include <QSystemTrayIcon>
99
#include "singleapplication.h"
@@ -61,7 +61,7 @@ public slots:
6161
// 用于在未登录时关闭窗口就退出
6262
int CURR_STATE;
6363

64-
QRegExpValidator *macValidator;
64+
QValidator *macValidator;
6565
DogcomController *dogcomController;
6666

6767
// 设置托盘中的注销按钮的可用性

mainwindow.ui

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<rect>
77
<x>0</x>
88
<y>0</y>
9-
<width>242</width>
9+
<width>250</width>
1010
<height>237</height>
1111
</rect>
1212
</property>
@@ -47,7 +47,7 @@
4747
</font>
4848
</property>
4949
<property name="echoMode">
50-
<enum>QLineEdit::Password</enum>
50+
<enum>QLineEdit::EchoMode::Password</enum>
5151
</property>
5252
</widget>
5353
</item>
@@ -60,7 +60,7 @@
6060
<string>MAC</string>
6161
</property>
6262
<property name="alignment">
63-
<set>Qt::AlignCenter</set>
63+
<set>Qt::AlignmentFlag::AlignCenter</set>
6464
</property>
6565
</widget>
6666
</item>
@@ -70,7 +70,7 @@
7070
<string>Account</string>
7171
</property>
7272
<property name="alignment">
73-
<set>Qt::AlignCenter</set>
73+
<set>Qt::AlignmentFlag::AlignCenter</set>
7474
</property>
7575
</widget>
7676
</item>
@@ -86,7 +86,7 @@
8686
<string>Password</string>
8787
</property>
8888
<property name="alignment">
89-
<set>Qt::AlignCenter</set>
89+
<set>Qt::AlignmentFlag::AlignCenter</set>
9090
</property>
9191
</widget>
9292
</item>
@@ -137,7 +137,7 @@
137137
<string>Your IP address will be shown here</string>
138138
</property>
139139
<property name="alignment">
140-
<set>Qt::AlignHCenter|Qt::AlignTop</set>
140+
<set>Qt::AlignmentFlag::AlignHCenter|Qt::AlignmentFlag::AlignTop</set>
141141
</property>
142142
</widget>
143143
</item>

singleinstance/singleapplication.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
#include <QtCore/QByteArray>
2727
#include <QtCore/QSharedMemory>
2828

29+
#include <cstdlib>
30+
2931
#include "singleapplication.h"
3032
#include "singleapplication_p.h"
3133

@@ -75,7 +77,7 @@ SingleApplication::SingleApplication( int &argc, char *argv[], bool allowSeconda
7577
}
7678

7779
InstancesInfo* inst = static_cast<InstancesInfo*>( d->memory->data() );
78-
QTime time;
80+
QElapsedTimer time;
7981
time.start();
8082

8183
// Make sure the shared memory block is initialised and in consistent state
@@ -92,8 +94,8 @@ SingleApplication::SingleApplication( int &argc, char *argv[], bool allowSeconda
9294
d->memory->unlock();
9395

9496
// Random sleep here limits the probability of a collision between two racing apps
95-
qsrand( QDateTime::currentMSecsSinceEpoch() % std::numeric_limits<uint>::max() );
96-
QThread::sleep( 8 + static_cast <unsigned long>( static_cast <float>( qrand() ) / RAND_MAX * 10 ) );
97+
srand( QDateTime::currentMSecsSinceEpoch() % std::numeric_limits<uint>::max() );
98+
QThread::sleep( 8 + static_cast <unsigned long>( static_cast <float>( rand() ) / RAND_MAX * 10 ) );
9799
}
98100

99101
if( inst->primary == false) {

singleinstance/singleapplication_p.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ SingleApplicationPrivate::~SingleApplicationPrivate()
8383
void SingleApplicationPrivate::genBlockServerName()
8484
{
8585
QCryptographicHash appData( QCryptographicHash::Sha256 );
86-
appData.addData( "SingleApplication", 17 );
86+
appData.addData( "SingleApplication");
8787
appData.addData( SingleApplication::app_t::applicationName().toUtf8() );
8888
appData.addData( SingleApplication::app_t::organizationName().toUtf8() );
8989
appData.addData( SingleApplication::app_t::organizationDomain().toUtf8() );
@@ -217,7 +217,7 @@ void SingleApplicationPrivate::connectToPrimary( int msecs, ConnectionType conne
217217
writeStream << blockServerName.toLatin1();
218218
writeStream << static_cast<quint8>(connectionType);
219219
writeStream << instanceNumber;
220-
quint16 checksum = qChecksum(initMsg.constData(), static_cast<quint32>(initMsg.length()));
220+
quint16 checksum = qChecksum(QByteArrayView(initMsg.constData(), static_cast<quint32>(initMsg.length())));
221221
writeStream << checksum;
222222

223223
// The header indicates the message length that follows
@@ -239,8 +239,8 @@ void SingleApplicationPrivate::connectToPrimary( int msecs, ConnectionType conne
239239
quint16 SingleApplicationPrivate::blockChecksum()
240240
{
241241
return qChecksum(
242-
static_cast <const char *>( memory->data() ),
243-
offsetof( InstancesInfo, checksum )
242+
QByteArrayView((static_cast <const char *>(memory->data())),
243+
offsetof( InstancesInfo, checksum ))
244244
);
245245
}
246246

@@ -365,7 +365,7 @@ void SingleApplicationPrivate::readInitMessageBody( QLocalSocket *sock )
365365
quint16 msgChecksum = 0;
366366
readStream >> msgChecksum;
367367

368-
const quint16 actualChecksum = qChecksum( msgBytes.constData(), static_cast<quint32>( msgBytes.length() - sizeof( quint16 ) ) );
368+
const quint16 actualChecksum = qChecksum( QByteArrayView(msgBytes.constData(), static_cast<quint32>( msgBytes.length() - sizeof( quint16 ) )) );
369369

370370
bool isValid = readStream.status() == QDataStream::Ok &&
371371
QLatin1String(latin1Name) == blockServerName &&

0 commit comments

Comments
 (0)