Browse Source

搜索用户

chengxr 1 year ago
parent
commit
cb3355f0e3
3 changed files with 42 additions and 19 deletions
  1. 32 11
      QFD/view/ExpertManageView.cpp
  2. 10 8
      QFD/view/ExpertManageView.h
  3. BIN
      bin/data/qfd.db

+ 32 - 11
QFD/view/ExpertManageView.cpp

@@ -3,7 +3,6 @@
 #include "QFDIcon.h"
 #include "MainWindow.h"
 
-#include "ExpertInfoWidget.h"
 #include "ExpertListWidget.h"
 
 #include "ExpertManager.h"
@@ -55,7 +54,8 @@ void ExpertManageView::initialize()
     QFont ft("Microsoft YaHei", 12);
     m_titleLabel->setFont(ft);
     m_hBoxLayout     = new QHBoxLayout();
-    m_searchLineEdit = new SearchLineEdit(this);
+    m_searchLineEdit = new LineEdit(this);
+    m_searchLineEdit->setIsClearButtonEnabled(true);
     m_searchLineEdit->setPlaceholderText("搜索");
     m_searchLineEdit->setMinimumWidth(300);
     m_addExpertButton = new PushButton("添加", NEWFLICON(FluentIcon, ADD), this);
@@ -95,6 +95,7 @@ void ExpertManageView::connectSignalsAndSlots()
             &ExpertManageView::slotListSelectionChanged);
     connect(m_expertListWidget, &ExpertListWidget::siganlItemDoubleClicked, this,
             &ExpertManageView::slotListItemDoubleClicked);
+    connect(m_searchLineEdit, &LineEdit::textChanged, this, &ExpertManageView::slotSearchTextChanged);
 }
 
 void ExpertManageView::setListButtonHidden(bool hidden)
@@ -134,13 +135,25 @@ void ExpertManageView::showExpertInfo(QFUser *user)
 
 void ExpertManageView::refreshList()
 {
+    qDeleteAll(m_userList);
     m_userList.clear();
     if (!DBServiceSet().QueryUserListNotAdmin(&m_userList)) {
         QFDAlert::showAlertWithCode(QF_CODE_DATA_ERROR, this);
         return;
     }
 
-    m_expertListWidget->showUsers(m_userList);
+    m_expertListWidget->showUsers(searchResult());
+}
+
+QList<QFUser *> ExpertManageView::searchResult() const
+{
+    QList<QFUser *> list;
+    for (QFUser *user : m_userList) {
+        if (user->userName.contains(m_searchLineEdit->text())) {
+            list.append(user);
+        }
+    }
+    return list;
 }
 
 void ExpertManageView::slotAddExpertClicked()
@@ -151,11 +164,12 @@ void ExpertManageView::slotAddExpertClicked()
 
 void ExpertManageView::slotDetailClicked()
 {
-    int row = m_expertListWidget->selectedRow();
-    if (row < 0 || row >= m_userList.count()) {
+    QList<QFUser *> list = searchResult();
+    int row              = m_expertListWidget->selectedRow();
+    if (row < 0 || row >= list.count()) {
         return;
     }
-    QFUser *user = m_userList[row];
+    QFUser *user = list[row];
 
     showExpertInfo(user);
 }
@@ -175,13 +189,19 @@ void ExpertManageView::slotListSelectionChanged()
 
 void ExpertManageView::slotListItemDoubleClicked(int row)
 {
-    if (row < 0 || row >= m_userList.count()) {
+    QList<QFUser *> list = searchResult();
+    if (row < 0 || row >= list.count()) {
         return;
     }
-    QFUser *user = m_userList[row];
+    QFUser *user = list[row];
     showExpertInfo(user);
 }
 
+void ExpertManageView::slotSearchTextChanged()
+{
+    m_expertListWidget->showUsers(searchResult());
+}
+
 void ExpertManageView::slotConfirmAddExpert()
 {
     QFUser *user = m_expertInfoWidget->user();
@@ -197,11 +217,12 @@ void ExpertManageView::slotConfirmAddExpert()
 
 void ExpertManageView::slotConfirmDeleteExpert()
 {
-    int row = m_expertListWidget->selectedRow();
-    if (row < 0 || row >= m_userList.count()) {
+    QList<QFUser *> list = searchResult();
+    int row              = m_expertListWidget->selectedRow();
+    if (row < 0 || row >= list.count()) {
         return;
     }
-    QFUser *user = m_userList[row];
+    QFUser *user = list[row];
 
     int code = ExpertManager::deleteUser(user->id);
     QFDAlert::showAlertWithCode(code, this);

+ 10 - 8
QFD/view/ExpertManageView.h

@@ -47,26 +47,28 @@ private:
 
     void refreshList();
 
+    QList<QFUser *> searchResult() const;
+
 private slots:
     void slotAddExpertClicked();
     void slotDetailClicked();
     void slotDeleteClicked();
     void slotListSelectionChanged();
     void slotListItemDoubleClicked(int row);
-
+    void slotSearchTextChanged();
     void slotConfirmAddExpert();
     void slotConfirmDeleteExpert();
 
 private:
     bool m_initilized = false;
 
-    QVBoxLayout *m_vBoxLayout        = nullptr;
-    QLabel *m_titleLabel             = nullptr;
-    QHBoxLayout *m_hBoxLayout        = nullptr;
-    SearchLineEdit *m_searchLineEdit = nullptr;
-    PushButton *m_addExpertButton    = nullptr;
-    ToolButton *m_detailButton       = nullptr;
-    ToolButton *m_deleteButton       = nullptr;
+    QVBoxLayout *m_vBoxLayout     = nullptr;
+    QLabel *m_titleLabel          = nullptr;
+    QHBoxLayout *m_hBoxLayout     = nullptr;
+    LineEdit *m_searchLineEdit    = nullptr;
+    PushButton *m_addExpertButton = nullptr;
+    ToolButton *m_detailButton    = nullptr;
+    ToolButton *m_deleteButton    = nullptr;
 
     ExpertInfoWidget *m_expertInfoWidget = nullptr;
     ExpertListWidget *m_expertListWidget = nullptr;

BIN
bin/data/qfd.db