我有一个来自互联网的QStacked Widget代码的例子,它为每个子级生成自己的布局(下面)
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()
现在我想收集所有这些数据,并在以后进行处理。例如,我如何知道选中了哪个复选框?此代码layout.addWidget(QCheckBox("Physics").stateChanged.connect(self.state_changed))
给我
进程已完成,退出代码为1073741819(0xC0000005)
当您编写时
layout.addWidget(QCheckBox("Physics").stateChanged.connect(self.state_changed))
它不会查找Physics复选框,但会创建一个新的复选框。因为您没有保留对它的Python引用,所以在离开构造函数后,它将被销毁。然而,它仍然与信号相连,从而导致不可预测的行为。
如果你想连接到原始复选框,你需要对其进行引用
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()
附言:我把self.Stack
改名为self.stack
。Python惯例是让类定义以大写字符和正则变量开头,函数以小写字母开头。