我有一个来自互联网的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))
给了我
Process finished with exit code -1073741819 (0xC0000005)
答案 0 :(得分:0)
写作时
layout.addWidget(QCheckBox("Physics").stateChanged.connect(self.state_changed))
没有查找“物理”复选框,但会创建一个新复选框。因为你没有保留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()
P.S。我将self.Stack
重命名为self.stack
。允许类定义以大写字符和常规变量开头并以小写形式起作用是一种Python约定。