QCompleter给pyqt5中的lineEdit带来奇怪的错误

时间:2019-07-09 18:51:33

标签: python pyqt5

我有两个lineEdits设置,其中一个具有QCompleter设置。当用户选择建议的文本时,两个lineEdits都应使用建议的文本填写。逗号右边的单词进入第一行编辑,左边的单词进入第二行编辑。目前,只有第二个lineEdit获取其文本集,但是第一个lineEdit仅在再次按下回车键之后才获取其文本集。 例如,如果用户选择“ chikin,pizza”并点击输入,则第二行Edit应该设置为chikin,第一个设置为pizza。目前,第二行编辑确实会说chikin,但是第一个仍然会说“ chikin,pizza”,直到您再次按下Enter键为止。我该如何解决它,使用户不必再按两次Enter键?

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_())

1 个答案:

答案 0 :(得分:1)

尝试一下:

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_())

enter image description here