@@ -68,6 +68,28 @@ ChatList::ChatList(QWidget* parent, QString localUserName, QString localUserGrou
6868 });
6969 });
7070
71+
72+ /* 搜索框状态变化 */
73+ connect (ui->leSearch , &QLineEdit::textChanged,
74+ this , [=](){
75+ /* 如果文本框中的内容为空,则显示所有的聊天按钮 */
76+ if (ui->leSearch ->text ().isEmpty ())
77+ {
78+ for (auto i : this ->vPair_OChat_BtnChat )
79+ {
80+ i.second ->show ();
81+ }
82+ return ;
83+ }
84+
85+ for (auto i : this ->vPair_OChat_BtnChat )
86+ {
87+ QString textOnBtn = i.second ->text ();
88+ if (isNeedHideBtn (textOnBtn)) i.second ->hide ();
89+ else i.second ->show ();
90+ }
91+ });
92+
7193}
7294
7395ChatList::~ChatList ()
@@ -82,6 +104,9 @@ void ChatList::addBtnChatInLayout(QToolButton* btn)
82104{
83105 if (nullptr == btn) return ;
84106
107+ /* 根据搜索框中的内容,显示或者隐藏按钮 */
108+ if (isNeedHideBtn (btn->text ())) btn->hide ();
109+
85110 ui->vLayout ->addWidget (btn); // 加到垂直布局中
86111}
87112
@@ -291,7 +316,6 @@ bool ChatList::setChatState(QString name, bool state)
291316}
292317
293318
294-
295319bool ChatList::updateBtnInvPair (QString name, QToolButton* btn)
296320{
297321 for (auto i : this ->vPair_OChat_BtnChat )
@@ -306,3 +330,22 @@ bool ChatList::updateBtnInvPair(QString name, QToolButton* btn)
306330 qDebug () << " [ERROR] File to update btn, ChatBox named" << name << " do not exits in local vPair_OChat_BtnChat" ;
307331 return false ;
308332}
333+
334+
335+ /* * 根据搜索框中的内容,并使用正则表达式,判断是否需要隐藏按钮
336+ * @brief isNeedHideBtn
337+ * @param btnText
338+ * @return true 代表需要隐藏
339+ */
340+ bool ChatList::isNeedHideBtn (QString textOnBtn)
341+ {
342+ /* 如果文本框中的内容为空,则不需要隐藏 */
343+ if (ui->leSearch ->text ().isEmpty ()) return false ;
344+
345+ QString strRegExp (" \\ S*" + ui->leSearch ->text () + " \\ S*" );
346+ QRegularExpression regExp;
347+ regExp.setPattern (strRegExp);
348+
349+ if (!regExp.match (textOnBtn).hasMatch ()) return true ;
350+ else return false ;
351+ }
0 commit comments