Setting up stateChanged signal in QStackedWidget, pyqt

Tags: , , ,



I have an example of QStacked Widget code from internet, which generates its own layout for each child (below)

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class stackedExample(QWidget):
    def __init__(self):
        super(stackedExample, self).__init__()
        self.leftlist = QListWidget()
        self.leftlist.insertItem(0, 'Contact')
        self.leftlist.insertItem(1, 'Personal')
        self.leftlist.insertItem(2, 'Educational')

        self.stack1 = QWidget()
        self.stack2 = QWidget()
        self.stack3 = QWidget()

        self.stack1UI()
        self.stack2UI()
        self.stack3UI()

        self.Stack = QStackedWidget(self)
        self.Stack.addWidget(self.stack1)
        self.Stack.addWidget(self.stack2)
        self.Stack.addWidget(self.stack3)

        hbox = QHBoxLayout(self)
        hbox.addWidget(self.leftlist)
        hbox.addWidget(self.Stack)

        self.setLayout(hbox)
        self.leftlist.currentRowChanged.connect(self.display)
        self.setGeometry(300, 50, 10, 10)
        self.setWindowTitle('StackedWidget demo')
        self.show()

    def stack1UI(self):
        layout = QFormLayout()
        layout.addRow("Name", QLineEdit())
        layout.addRow("Address", QLineEdit())
        # self.setTabText(0,"Contact Details")
        self.stack1.setLayout(layout)

    def stack2UI(self):
        layout = QFormLayout()
        sex = QHBoxLayout()
        sex.addWidget(QRadioButton("Male"))
        sex.addWidget(QRadioButton("Female"))
        layout.addRow(QLabel("Sex"), sex)
        layout.addRow("Date of Birth", QLineEdit())

        self.stack2.setLayout(layout)

    def stack3UI(self):
        layout = QHBoxLayout()
        layout.addWidget(QLabel("subjects"))
        layout.addWidget(QCheckBox("Physics"))
        layout.addWidget(QCheckBox("Maths"))
        self.stack3.setLayout(layout)

    def state_changed(self):
        pass

    def display(self, i):
        self.Stack.setCurrentIndex(i)


def main():
    app = QApplication(sys.argv)
    ex = stackedExample()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

Now I want to collect data from all of them and process it later. How can I know which checkbox was checked, for example? This code layout.addWidget(QCheckBox(“Physics”).stateChanged.connect(self.state_changed))

gives me

Process finished with exit code -1073741819 (0xC0000005)

Answer

When you write

layout.addWidget(QCheckBox("Physics").stateChanged.connect(self.state_changed))

that doesn’t lookup the Physics checkbox but it creates a new checkbox. Because you don’t keep a Python reference to it, it will be destructed after you leave the constructor. However, it is still connected to a signal, which leads to unpredictable behavior.

If you want to connect to the original checkbox you will need to make a reference to it. Like so:

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class stackedExample(QWidget):
    def __init__(self):
        super(stackedExample, self).__init__()
        self.leftlist = QListWidget()
        self.leftlist.insertItem(0, 'Contact')
        self.leftlist.insertItem(1, 'Personal')
        self.leftlist.insertItem(2, 'Educational')

        self.stack1 = QWidget()
        self.stack2 = QWidget()
        self.stack3 = QWidget()

        self.stack1UI()
        self.stack2UI()
        self.stack3UI()

        # Renamed self.stack to self.stack since the convention is to start
        # class names with a capital but regular variables with a lower case.
        self.stack = QStackedWidget(self) 
        self.stack.addWidget(self.stack1)
        self.stack.addWidget(self.stack2)
        self.stack.addWidget(self.stack3)

        hbox = QHBoxLayout(self)
        hbox.addWidget(self.leftlist)
        hbox.addWidget(self.stack)

        self.setLayout(hbox)
        self.leftlist.currentRowChanged.connect(self.display)
        self.setGeometry(300, 50, 10, 10)
        self.setWindowTitle('StackedWidget demo')
        self.show()

    def stack1UI(self):
        layout = QFormLayout()
        layout.addRow("Name", QLineEdit())
        layout.addRow("Address", QLineEdit())
        # self.setTabText(0,"Contact Details")
        self.stack1.setLayout(layout)

    def stack2UI(self):
        layout = QFormLayout()
        sex = QHBoxLayout()
        sex.addWidget(QRadioButton("Male"))
        sex.addWidget(QRadioButton("Female"))
        layout.addRow(QLabel("Sex"), sex)
        layout.addRow("Date of Birth", QLineEdit())

        self.stack2.setLayout(layout)

    def stack3UI(self):
        layout = QHBoxLayout()
        layout.addWidget(QLabel("subjects"))
        self.physicsCheckBox = QCheckBox("Physics")
        layout.addWidget(self.physicsCheckBox)
        self.physicsCheckBox.stateChanged.connect(self.physicsCheckBoxStateChanged)

        layout.addWidget(QCheckBox("Maths"))
        self.stack3.setLayout(layout)


    def physicsCheckBoxStateChanged(self, state):
        isChecked = bool(state) # Convert from Qt.CheckState
        print("physicsCheckBox: {}".format(isChecked))

    def display(self, i):
        self.stack.setCurrentIndex(i)


def main():
    app = QApplication(sys.argv)
    ex = stackedExample()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

P.S. I renamed self.Stack to self.stack. It is a Python convention to let class definitions start with upper case characters and regular variables and function with lower case.



Source: stackoverflow