Skip to content
Advertisement

Setting up stateChanged signal in QStackedWidget, pyqt

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

JavaScript

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)

Advertisement

Answer

When you write

JavaScript

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:

JavaScript

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.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement