Search code examples
pythonpyqtpyqt5qcombobox

Qcombobox in PyQt, How could I set the options in one combo box which depends on the choice of other combo box?


For example, the first combo box with option A and option B. The second combo box contains option 1,2,3,4,5 if I select option A in 1st combo box. The second combo box contains option a,b,c,d,e if I select option B in 1st combo box.

How could I do this in PyQt5? I have searched Qcombobox class, there is 2 class activated and currentIndexChanged, but do not know which one I need to use and how to use it in PyQt5.


Solution

  • This is how I would do it:

    import sys
    from PyQt5 import QtWidgets
    
    
    class ComboWidget(QtWidgets.QWidget):
    
        def __init__(self, parent=None):
            super(ComboWidget, self).__init__(parent)
            self.setGeometry(50, 50, 200, 200)
    
            layout = QtWidgets.QVBoxLayout(self)
    
            self.comboA = QtWidgets.QComboBox()
            # The second addItem parameter is itemData, which we will retrieve later
            self.comboA.addItem('A', ['1', '2', '3', '4'])
            self.comboA.addItem('B', ['a', 'b', 'c', 'd', 'e'])
            self.comboA.currentIndexChanged.connect(self.indexChanged)
            layout.addWidget(self.comboA)
    
            self.comboB = QtWidgets.QComboBox()
            # We could have added the 1,2,3,4 items directly, but the following
            # is a bit more generic in case the combobox starts with a different
            # index for some reason:
            self.indexChanged(self.comboA.currentIndex())
            layout.addWidget(self.comboB)
    
            self.show()
    
        def indexChanged(self, index):
            self.comboB.clear()
            data = self.comboA.itemData(index)
            if data is not None:
                self.comboB.addItems(data)
    
    
    def main():
        app = QtWidgets.QApplication(sys.argv)
        w = ComboWidget()
        sys.exit(app.exec_())
    
    if __name__ == '__main__':
        main()
    

    That way, your code is a bit more future-proof, as it would allow you to add / modify / remove the items in any of the dropdowns quite easily if you wanted to do so at some point.