From 1f6a42e4ebed4c4f7b9e18ea4d18c3df3c467114 Mon Sep 17 00:00:00 2001 From: Cheukfung Date: Wed, 12 Jul 2023 01:01:39 +0800 Subject: [PATCH] fix #344 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复lineEdit添加QCompleter无法触发activated事件 --- qfluentwidgets/components/widgets/line_edit.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/qfluentwidgets/components/widgets/line_edit.py b/qfluentwidgets/components/widgets/line_edit.py index 9ade43e..67d7bd5 100644 --- a/qfluentwidgets/components/widgets/line_edit.py +++ b/qfluentwidgets/components/widgets/line_edit.py @@ -127,6 +127,7 @@ class LineEdit(QLineEdit): # create menu if not self._completerMenu: self._completerMenu = CompleterMenu(self) + self._completerMenu.activated.connect(self._completer.activated) # add menu items self.completer().setCompletionPrefix(self.text()) @@ -164,6 +165,8 @@ class LineEdit(QLineEdit): class CompleterMenu(RoundMenu): """ Completer menu """ + activated = pyqtSignal(str) + def __init__(self, lineEdit: LineEdit): super().__init__() self.items = [] @@ -192,7 +195,7 @@ class CompleterMenu(RoundMenu): # add items for i in items: - self.addAction(QAction(i, triggered=lambda c, x=i: self.lineEdit.setText(x))) + self.addAction(QAction(i, triggered=lambda c, x=i: self.__onItemSelected(x))) return True @@ -207,11 +210,15 @@ class CompleterMenu(RoundMenu): if e.key() == Qt.Key_Escape: self.close() if e.key() in [Qt.Key_Enter, Qt.Key_Return] and self.view.currentRow() >= 0: - self.lineEdit.setText(self.view.currentItem().text()) + self.__onItemSelected(self.view.currentItem().text()) self.close() return super().eventFilter(obj, e) + def __onItemSelected(self, text): + self.lineEdit.setText(text) + self.activated.emit(text) + def popup(self): """ show menu """ if not self.items: -- GitLab