Skip to content

Commit b780d65

Browse files
Merge pull request #88 from NekoSilverFox/MengJianing
Add Search function
2 parents a8cf276 + 593d689 commit b780d65

File tree

7 files changed

+59
-4
lines changed

7 files changed

+59
-4
lines changed

PolyChatApp/PolyChatApp.pro

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,5 @@ else: unix:!android: target.path = /opt/$${TARGET}/bin
4040

4141
RESOURCES += \
4242
resource.qrc
43+
44+
RC_ICONS = logo_fox.ico

PolyChatApp/bll_polychat.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ static void initAndShowChatList(QWidget* parent)
9191
chatList->show();
9292
}
9393

94-
9594
}
9695

9796
#endif // BLL_POLYCHAT_H

PolyChatApp/logo_fox.ico

32.2 KB
Binary file not shown.

PolyChatApp/signaltype.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ enum SignalType
1212

1313
};
1414

15-
static const int BORDCAST_TIME_STEP = 3000; // 广播信息的时间间隔
15+
static const int BORDCAST_TIME_STEP = 1000; // 广播信息的时间间隔
1616

1717
#endif // SIGNALTYPE_H

PolyChatApp/uil_chatboxwidget.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,15 @@ void ChatBoxWidget::receiveUDPMessage()
251251
dataStream >> localIpAddress_6; // 第6段:发送信号用户ip
252252
dataStream >> msg_7; // 第7段:具体内容 QString
253253

254+
qDebug() << "收到消息:" << SignalType::Msg
255+
<< signalType_1
256+
<< chatName_2
257+
<< chatPort_3
258+
<< localUserName_4
259+
<< localUserGroupNumber_5
260+
<< localIpAddress_6
261+
<< msg_7;
262+
254263
switch (signalType_1) {
255264
case SignalType::Msg:
256265
// 追加聊天记录

PolyChatApp/uil_chatlist.cpp

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

7395
ChatList::~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-
295319
bool 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+
}

PolyChatApp/uil_chatlist.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <QWidget>
77
#include <QUdpSocket>
88
#include <QToolButton>
9+
#include <QRegularExpression>
910

1011
namespace Ui {
1112
class ChatList;
@@ -31,11 +32,12 @@ class ChatList : public QWidget
3132
void addBtnChatInLayout(QToolButton* btn); // 添加按钮对象到 Layout
3233

3334
QToolButton* getNewBtn(QString btn_text, qint16 port, bool isOpen);
34-
qint16 getRandomPort(); //获取一个不重复的随机端口号
35+
qint16 getRandomPort(); //获取一个不重复的随机端口号
3536

3637
bool setChatState(QString name, bool state); // 设置聊天窗口为打开或者关闭
3738
bool updateBtnInvPair(QString name, QToolButton* btn);
3839

40+
bool isNeedHideBtn(QString textOnBtn); // 根据正则表达式,判断是否需要隐藏按钮
3941
private:
4042
Ui::ChatList *ui;
4143

0 commit comments

Comments
 (0)