在 QStackedWidget 中设置 stateChanged 信号,pyqt
Setting up stateChanged signal in QStackedWidget, pyqt
我有一个来自互联网的 QStacked Widget 代码示例,它为每个 child(下)
生成自己的布局
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("物理").stateChanged.connect(self.state_changed))
给我
Process finished with exit code -1073741819 (0xC0000005)
当你写
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 约定让 class 定义以大写字符开头,常规变量和函数以小写字母开头。
我有一个来自互联网的 QStacked Widget 代码示例,它为每个 child(下)
生成自己的布局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("物理").stateChanged.connect(self.state_changed))
给我
Process finished with exit code -1073741819 (0xC0000005)
当你写
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 约定让 class 定义以大写字符开头,常规变量和函数以小写字母开头。