QCompleter giving weird bug to lineEdit in pyqt5(QCompleter 给 pyqt5 中的 lineEdit 带来奇怪的错误)
问题描述
我设置了两个 lineEdit,其中一个设置了 QCompleter.当用户选择建议的文本时,两个 lineEdits 都应填写建议的文本.逗号右边的词进入第一行编辑,左边的词进入第二行编辑.目前,只有第二个 lineEdit 获得了它的文本集,但第一个 lineEdit 在再次按 Enter 后才获得它的文本集.例如,如果用户选择chikin,pizza"并点击进入第二行编辑应该设置为chikin,第一个设置为pizza.目前,第二行编辑确实说 chikin,但第一个仍然说chikin,pizza",直到您再次按 Enter 键.我怎样才能解决它,以便用户不必按两次回车?
I have two lineEdits set up where one of them has a QCompleter setup to it. When a user selects the suggested text, both lineEdits should be filled in with the suggested text. The word to the right of the comma goes into the first lineedit and the word to the left goes into the second lineEdit. At the moment, only the second lineEdit gets its text set, but the first one only gets its text set after hitting enter again. For example, if a user selects "chikin, pizza" and hits enter the second lineEdit should be set to chikin and the first one set to pizza. Currently, the second lineedit does say chikin, but the first one still says "chikin, pizza" until you hit enter again. How can i fix it so that the user doesn't have to hit enter twice?
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QCompleter
class Ui_MainWindow(object):
def on_button(self):
print('Button clicked')
fullName = self.lineEdit.text()
nameList = fullName.split(', ')
firstName = nameList[1]
lastName = nameList[0]
self.lineEdit_2.setText(lastName)
self.lineEdit.setText(firstName)
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(439, 254)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(20, 90, 180, 25))
self.lineEdit.setObjectName("lineEdit")
self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_2.setGeometry(QtCore.QRect(240, 90, 180, 25))
self.lineEdit_2.setObjectName("lineEdit_2")
food = ["pizza, chikin", "chikin, pizza", "chikin, pizza pizza", "chikin, pizza", "fried, pizza"]
completer = QCompleter(food)
completer.setCaseSensitivity(QtCore.Qt.CaseInsensitive)
completer.setFilterMode(QtCore.Qt.MatchContains)
self.lineEdit.setCompleter(completer)
self.lineEdit.editingFinished.connect(lambda: self.on_button())
MainWindow.setCentralWidget(self.centralwidget)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
推荐答案
试试看:
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QCompleter
class Ui_MainWindow(object):
# def on_button(self):
def on_button(self, text): # +++
nameList = text.split(', ')
self.lineEdit_2.setText(nameList[0])
# self.lineEdit.setText(nameList[1])
QtCore.QTimer.singleShot(200, lambda: self.lineEdit.setText(nameList[1])) # +++
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(439, 254)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(20, 90, 180, 25))
self.lineEdit.setObjectName("lineEdit")
self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_2.setGeometry(QtCore.QRect(240, 90, 180, 25))
self.lineEdit_2.setObjectName("lineEdit_2")
food = ["pizza, chikin", "chikin, pizza", "chikin, pizza pizza", "chikin, pizza", "fried, pizza"]
completer = QCompleter(food)
completer.setCaseSensitivity(QtCore.Qt.CaseInsensitive)
completer.setFilterMode(QtCore.Qt.MatchContains)
self.lineEdit.setCompleter(completer)
# self.lineEdit.editingFinished.connect(lambda: self.on_button())
completer.activated.connect(self.on_button) # +++
MainWindow.setCentralWidget(self.centralwidget)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
这篇关于QCompleter 给 pyqt5 中的 lineEdit 带来奇怪的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:QCompleter 给 pyqt5 中的 lineEdit 带来奇怪的错误
- 我如何卸载 PyTorch? 2022-01-01
- 我如何透明地重定向一个Python导入? 2022-01-01
- 使用 Cython 将 Python 链接到共享库 2022-01-01
- 如何使用PYSPARK从Spark获得批次行 2022-01-01
- ";find_element_by_name(';name';)";和&QOOT;FIND_ELEMENT(BY NAME,';NAME';)";之间有什么区别? 2022-01-01
- YouTube API v3 返回截断的观看记录 2022-01-01
- CTR 中的 AES 如何用于 Python 和 PyCrypto? 2022-01-01
- 使用公司代理使Python3.x Slack(松弛客户端) 2022-01-01
- 计算测试数量的Python单元测试 2022-01-01
- 检查具有纬度和经度的地理点是否在 shapefile 中 2022-01-01