t0m3k
t0m3k

Reputation: 317

populating pyqt5 inputs from dictionary selection

I'm having issues with my pyqt5 combobox dictionary. How can I target the secondary pieces of data (e-mail, tel#) and send them to other inputs once the primary data is selected?

Dictionary:

autocompleteList2 = {
    'James': ['[email protected]', '410-555-5555']
}

model2 = QStringListModel()
model2.setStringList(autocompleteList2)
completer2 = QCompleter()
completer2.setModel(model2)
self.comboBox_4.setCompleter(completer2)
self.comboBox_4.addItems(autocompleteList2)

This only pulls the name "James." I want my QLineEdit_1 to populate with "[email protected]" and my QLineEdit_2 to populate with "410-555-5555" after I select James in comboBox_4. How can I do this? :/

Upvotes: 2

Views: 2278

Answers (2)

S. Nick
S. Nick

Reputation: 13661

In this case, you don’t need a QCompleter. Try the example below:

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': ['[email protected]', '410-111-1111'],
    'James-2': ['[email protected]', '410-222-2222'],
    'James-3': ['[email protected]', '410-333-3333'],
}

if __name__ == '__main__':
    import sys
    app  = QtWidgets.QApplication(sys.argv)
    w = Window(autocompleteList2)
    w.show()
    sys.exit(app.exec_())

enter image description here


Try something like this:

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': ['[email protected]', '410-111-1111'],
    'James-2': ['[email protected]', '410-222-2222'],
    'James-3': ['[email protected]', '410-333-3333'],
}

if __name__ == '__main__':
    import sys
    app  = QtWidgets.QApplication(sys.argv)
    w = Window(autocompleteList2)
    w.show()
    sys.exit(app.exec_())

Upvotes: 2

Astrom
Astrom

Reputation: 767

You have to connect your combobox to an event with:

your_comboBox.activated.connect(your_function)

Then in your function you update the two QlineEdits.

Upvotes: 0

Related Questions