From 21c47864d02302dd7bcdbdc480dadf7beeb7e0d7 Mon Sep 17 00:00:00 2001 From: zhangjiaping Date: Sat, 26 Dec 2020 20:54:01 +0800 Subject: [PATCH] feat(Searchbar): Add qtimer. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Description: 添加搜索框延时以避免搜索请求发送过于频繁 Log: 添加搜索框延时以避免搜索请求发送过于频繁 --- src/input-box.cpp | 28 ++++++++++++++++++++++++++-- src/input-box.h | 3 +++ src/mainwindow.cpp | 11 +++++++++-- 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/src/input-box.cpp b/src/input-box.cpp index 7c2273b..7f4bed5 100644 --- a/src/input-box.cpp +++ b/src/input-box.cpp @@ -1,4 +1,5 @@ #include "input-box.h" + /** * @brief ukui-search顶部搜索界面 */ @@ -62,12 +63,35 @@ UkuiSearchBarHLayout::UkuiSearchBarHLayout() this->setAlignment(m_queryLineEdit,Qt::AlignCenter); this->addWidget(m_queryLineEdit); - connect(m_queryLineEdit, SIGNAL(textChanged(QString)), SIGNAL(textChanged(QString))); +// connect(m_queryLineEdit, SIGNAL(textChanged(QString)), SIGNAL(textChanged(QString))); + m_timer = new QTimer; + QObject::connect(m_timer, &QTimer::timeout, this, [ = ](){ + m_timer->stop(); + Q_EMIT this->textChanged(m_queryLineEdit->text()); + }); + connect(m_queryLineEdit, &UKuiSearchLineEdit::textChanged, this, [ = ](QString text) { + if (m_isEmpty) { + m_isEmpty = false; + Q_EMIT this->textChanged(text); + } else { + if (text == "") { + m_isEmpty = true; + Q_EMIT this->textChanged(m_queryLineEdit->text()); + m_timer->stop(); + return; + } + m_timer->stop(); + m_timer->start(0.2 * 1000); + } + }); } UkuiSearchBarHLayout::~UkuiSearchBarHLayout() { - + if (m_timer) { + delete m_timer; + m_timer = NULL; + } } /** diff --git a/src/input-box.h b/src/input-box.h index ea0a2e4..ab9872c 100644 --- a/src/input-box.h +++ b/src/input-box.h @@ -5,6 +5,7 @@ #include #include #include +#include class UKuiSearchLineEdit; @@ -36,6 +37,8 @@ public: void clearText(); private: void initUI(); + bool m_isEmpty = true; + QTimer * m_timer = nullptr; UKuiSearchLineEdit *m_queryLineEdit=nullptr; diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 43dabc8..648b877 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -28,6 +28,8 @@ #include #include #include +#include "setting-match.h" +#include "app-match.h" #include "kwindowsystem.h" #include "file-utils.h" @@ -215,13 +217,18 @@ void MainWindow::searchContent(QString searchcontent){ QVector types; QVector lists; + AppMatch * appMatchor = new AppMatch(this); + SettingsMatch * settingMatchor = new SettingsMatch(this); + //测试用数据 QStringList list; - list<<"/usr/share/applications/peony.desktop"<<"/usr/share/applications/ukui-control-center.desktop"<<"/usr/share/applications/wps-office-pdf.desktop"; + list = appMatchor->startMatchApp(searchcontent); +// list<<"/usr/share/applications/peony.desktop"<<"/usr/share/applications/ukui-control-center.desktop"<<"/usr/share/applications/wps-office-pdf.desktop"; QStringList list2; list2<<"/home/zjp/下载/搜索结果.png"<<"/home/zjp/下载/显示不全.mp4"<<"/home/zjp/下载/dmesg.log"<<"/home/zjp/下载/WiFi_AP选择.docx"; QStringList list3; - list3<<"About/关于/计算机属性"<<"Area/语言和地区/货币单位"<<"Datetime/时间和日期/手动更改时间"<<"Theme/主题/图标主题"; + list3 = settingMatchor->matchstart(searchcontent); +// list3<<"About/关于/计算机属性"<<"Area/语言和地区/货币单位"<<"Datetime/时间和日期/手动更改时间"<<"Theme/主题/图标主题"; types.append(SearchItem::SearchType::Apps); types.append(SearchItem::SearchType::Settings); types.append(SearchItem::SearchType::Files);