Setting up stateChanged signal in QStackedWidget, pyqt

给你一囗甜甜゛ 提交于 2019-12-13 02:26:16

问题


I have an example of QStacked Widget code from internet, wich generates 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 wich 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)

回答1:


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.



来源:https://stackoverflow.com/questions/36515497/setting-up-statechanged-signal-in-qstackedwidget-pyqt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!