我的pyqt5组合框字典出现问题。一旦选择了主要数据,如何定位次要数据(电子邮件,电话号码)并将其发送到其他输入?
字典:
autocompleteList2 = {
'James': ['James@Gmail.com', '410-555-5555']
}
model2 = QStringListModel()
model2.setStringList(autocompleteList2)
completer2 = QCompleter()
completer2.setModel(model2)
self.comboBox_4.setCompleter(completer2)
self.comboBox_4.addItems(autocompleteList2)
这只会拉名字“詹姆斯”。在QLineEdit_1
中选择James之后,我希望我的QLineEdit_2
用“ James@Gmail.com”填充,而我的comboBox_4
用“ 410-555-5555”填充。我怎样才能做到这一点? :/
答案 0 :(得分:1)
在这种情况下,您不需要QCompleter
。请尝试以下示例:
from PyQt5 import QtCore, QtGui, QtWidgets
class 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_())
尝试这样的事情:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.setEnabled(True)
MainWindow.resize(480, 637)
MainWindow.setToolButtonStyle(QtCore.Qt.ToolButtonIconOnly)
MainWindow.setAnimated(True)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
# ...
self.lineEdit_1 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.comboBox_4 = QtWidgets.QComboBox(self.centralwidget)
# ...
MainWindow.setCentralWidget(self.centralwidget)
class Window(QtWidgets.QMainWindow, Ui_MainWindow): # + Ui_MainWindow
def __init__(self, cList):
super().__init__()
self.setupUi(self) # +++
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)
# self.comboBox_4 = QtWidgets.QComboBox()
self.comboBox_4.addItems(sorted(cList.keys()))
self.comboBox_4.activated[str].connect(self.onActivatedText)
# layoutV = QtWidgets.QVBoxLayout()
layoutV = QtWidgets.QVBoxLayout(self.centralwidget) # self.centralwidget
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_())
答案 1 :(得分:0)
您必须通过以下方式将combobox
连接到event
:
your_comboBox.activated.connect(your_function)
然后在函数中更新两个QlineEdits
。