feat(Searchbar): Add qtimer.

Description: 添加搜索框延时以避免搜索请求发送过于频繁

Log: 添加搜索框延时以避免搜索请求发送过于频繁
This commit is contained in:
zhangjiaping 2020-12-26 20:54:01 +08:00
parent ded6461ad9
commit 21c47864d0
3 changed files with 38 additions and 4 deletions

View File

@ -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;
}
}
/**

View File

@ -5,6 +5,7 @@
#include <QtDBus/QtDBus>
#include <QPainter>
#include <QAction>
#include <QTimer>
class UKuiSearchLineEdit;
@ -36,6 +37,8 @@ public:
void clearText();
private:
void initUI();
bool m_isEmpty = true;
QTimer * m_timer = nullptr;
UKuiSearchLineEdit *m_queryLineEdit=nullptr;

View File

@ -28,6 +28,8 @@
#include <QStyleOption>
#include <KWindowEffects>
#include <QPixmap>
#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<int> types;
QVector<QStringList> 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);