pyqt5 从下拉列选后一一添加到lineedit?
发布网友
发布时间:2022-04-21 09:56
我来回答
共1个回答
热心网友
时间:2023-11-09 15:13
c++下面是将数据读取出来,然后添加到qlistview中,这样可以列表型显示。然后把这个列表绘制到qlineedit下面。
在这种情况下,您不需要QCompleter。请尝试以下示例:
from PyQt5 import QtCore, QtGui, QtWidgetsclass Window(QtWidgets.QMainWindow):
def __init__(self, cList):
super().__init__()
self.cList = cList
self.lineEdit_1 = QtWidgets.QLineEdit()
self.lineEdit_2 = QtWidgets.QLineEdit()
layoutH = QtWidgets.QHBoxLayout()
layoutH.addWidget(self.lineEdit_1)
layoutH.addWidget(self.lineEdit_2)# completer = QtWidgets.QCompleter(self)# model = QtCore.QStringListModel()# completer.setModel(model)
self.comboBox_4 = QtWidgets.QComboBox()# self.comboBox_4.setCompleter(completer)
self.comboBox_4.addItems(sorted(cList.keys()))
self.comboBox_4.activated[str].connect(self.onActivatedText)
layoutV = QtWidgets.QVBoxLayout()
layoutV.addLayout(layoutH)
layoutV.addWidget(self.comboBox_4)
centralWidget = QtWidgets.QWidget()
centralWidget.setLayout(layoutV)
self.setCentralWidget(centralWidget)
@QtCore.pyqtSlot(str)
def onActivatedText(self, text):
self.lineEdit_1.setText(self.cList[text][0])
self.lineEdit_2.setText(self.cList[text][1])autocompleteList2 = {
'James-1': ['James111@Gmail.com', '410-111-1111'],
'James-2': ['James222@Gmail.com', '410-222-2222'],
'James-3': ['James333@Gmail.com', '410-333-3333'],}if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = Window(autocompleteList2)
w.show()
sys.exit(app.exec_())