问题描述
我设置了两个 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_())