Search code examples
pyqtqcomboboxqcheckbox

Change QComboBox items with QCheckBox from different class


I am trying to figure out how to change the items in QComboBox from another class. I have tried several ways already including trying to populate the items from the other class as well. In my example below I am embedding the class Widget1 which has a QCheckBox into MainWindow and trying to change the list items when it is checked but it is not working. It will print connect but then nothing else. Can someone explain what I am doing wrong? Thanks.

import sys
from PyQt5.QtWidgets import QWidget, QComboBox, QHBoxLayout, QApplication, QVBoxLayout, QCheckBox

class MainWindow(QWidget):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.widget1 = Widget1
        self.hbox = QHBoxLayout()
        self.Combo_Box = QComboBox()
        self.hbox.addWidget(self.Combo_Box)
        self.hbox.addWidget(self.widget1())
        self.setLayout(self.hbox)
        self.list = ['1','2','3','4','5','6']
        self.Combo_Box.addItems(self.list)

    def change_ComboBox(self):
        print("connected")
        self.Combo_Box.clear()
        self.list2 = ['7', '8', '9', '10', '11', '12']
        self.Combo_Box.addItems(self.list2)

class Widget1(QWidget):
    def __init__(self):
        super(Widget1, self).__init__()
        self.vbox = QVBoxLayout()
        self.check_Box = QCheckBox(self)
        self.vbox.addWidget(self.check_Box)
        self.check_Box.clicked.connect(self.send_change_ComboBox)

    def send_change_ComboBox(self):
        self.mainwindow = MainWindow()
        if self.check_Box.isChecked():
            self.mainwindow.change_ComboBox()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    wnd = MainWindow()
    wnd.show()
    app.exec_()

Solution

  • Try it:

    import sys
    from PyQt5.QtWidgets import QWidget, QComboBox, QHBoxLayout, QApplication, QVBoxLayout, QCheckBox
    
    class MainWindow(QWidget):
        def __init__(self):
            super(MainWindow, self).__init__()
            self.widget1   = Widget1(self)            
            self.hbox      = QHBoxLayout()
            self.Combo_Box = QComboBox()
            self.hbox.addWidget(self.Combo_Box)
            self.hbox.addWidget(self.widget1)     
            self.setLayout(self.hbox)
            self.initUi()
    
        def initUi(self): 
            self.Combo_Box.clear()    
            self.list = ['1','2','3','4','5','6']
            self.Combo_Box.addItems(self.list)
    
        def change_ComboBox(self):
            print("connected")
            self.Combo_Box.clear()
            self.list2 = ['7', '8', '9', '10', '11', '12']
            self.Combo_Box.addItems(self.list2)
    
    class Widget1(QWidget):
        def __init__(self, parent=None):
            super(Widget1, self).__init__(parent)
            self.parent = parent
    
            self.vbox      = QVBoxLayout()
            self.check_Box = QCheckBox(self)
            self.vbox.addWidget(self.check_Box)
            self.check_Box.clicked.connect(self.send_change_ComboBox)
    
        def send_change_ComboBox(self):
            if self.check_Box.isChecked():
                self.parent.change_ComboBox()
            else:
                self.parent.initUi()
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        wnd = MainWindow()
        wnd.show()
        app.exec_()
    

    enter image description here