From 9d7e067798f5ca471b905a8d36e5e54f9ea63064 Mon Sep 17 00:00:00 2001 From: mammonsama666 <1216715129@qq.com> Date: Fri, 11 Dec 2020 14:53:20 +0800 Subject: [PATCH] feat(mainwindow): Optimize code logic. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Description: 优化代码逻辑 Log: 优化代码逻辑 --- src/backthread.cpp | 4 +++- src/mainwindow.cpp | 28 ++++++++++++++++++++-------- src/mainwindow.h | 1 + 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/backthread.cpp b/src/backthread.cpp index 9bc02a97..6bec1499 100644 --- a/src/backthread.cpp +++ b/src/backthread.cpp @@ -75,7 +75,8 @@ IFace* BackThread::execGetIface() QString iname = lastStr.left(index2); QString istateStr = lastStr.mid(index2).trimmed(); - if (type == "ethernet") { + //只要存在一个有线设备已连接,就不再扫描其他有线设备状态,避免有线被误断开 + if (type == "ethernet" && iface->lstate != 0) { // if type is wired network iface->lname = iname; @@ -434,5 +435,6 @@ void BackThread::disConnLanOrWifi(QString type) kylin_network_set_con_down(strSlist.toUtf8().data()); } } + emit btFinish(); pclose(p_file); } diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 7e036ddb..0ee7e0e9 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -494,10 +494,16 @@ void MainWindow::initNetwork() BackThread *m_bt = new BackThread(); IFace *m_iface = m_bt->execGetIface(); - m_bt->disConnLanOrWifi("ethernet"); - sleep(1); - m_bt->disConnLanOrWifi("ethernet"); - sleep(1); + //写成信号监听自动执行,避免阻塞主线程 + disconnect_time = 1; + connect (m_bt, &BackThread::btFinish, this, [ = ](){ + disconnect_time ++; + if (disconnect_time <= 3) { + m_bt->disConnLanOrWifi("ethernet"); + } else { + m_bt->disconnect(); + } + }); m_bt->disConnLanOrWifi("ethernet"); delete m_iface; @@ -918,10 +924,16 @@ void MainWindow::onDeleteLan() { deleteLanTimer->stop(); BackThread *btn_bt = new BackThread(); - btn_bt->disConnLanOrWifi("ethernet"); - sleep(1); - btn_bt->disConnLanOrWifi("ethernet"); - sleep(1); + //写成信号监听自动执行,避免阻塞主线程 + disconnect_time = 1; + connect (btn_bt, &BackThread::btFinish, this, [ = ](){ + disconnect_time ++; + if (disconnect_time <= 3) { + btn_bt->disConnLanOrWifi("ethernet"); + } else { + btn_bt->disconnect(); + } + }); btn_bt->disConnLanOrWifi("ethernet"); btn_bt->deleteLater(); diff --git a/src/mainwindow.h b/src/mainwindow.h index 08c87b3c..f0bdac04 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -268,6 +268,7 @@ private: int currentIconIndex; int activeWifiSignalLv; + int disconnect_time = 0; long int start_rcv_rates = 0; //保存开始时的流量计数 long int end_rcv_rates = 0; //保存结束时的流量计数