PySide - 如何将信号连接到通过在另一个方法中调用的方法创建的小部件
PySide - How to connect a signal to a widget that was created through a method being called inside another method
我对编程还很陌生,所以请耐心等待。我正在尝试在 Autodesk Maya 中创建一个简单的脚本。我创建了一个并排设置两个复选框的方法(见下文)...
def checkboxLayout(self, name1, text1, name2, text2, parentLayout, initState):
##create and add horizontal layout
layout = QtGui.QHBoxLayout()
parentLayout.addLayout(layout)
width = 75
name1 = QtGui.QCheckBox(text1)
layout.addWidget(name1)
name1.setMinimumWidth(width)
name1.setMaximumWidth(width)
name1.setChecked(initState)
name2 = QtGui.QCheckBox(text2)
layout.addWidget(name2)
name2.setMinimumWidth(width)
name2.setMaximumWidth(width)
后来我调用了这个方法两次,以免我不得不将相同的大代码块写出两次...
def createLayout(self):
##Layout
mainLayout = QtGui.QVBoxLayout()
mainLayout.addWidget(self.title)
mainLayout.addSpacerItem(self.titleSpacer)
mainLayout.addWidget(self.oldLabel)
self.checkboxLayout("selection_CB", "Selection", "name_CB", "Name", mainLayout, True)
mainLayout.addWidget(self.textbox1)
mainLayout.addSpacerItem(self.midSpacer)
mainLayout.addWidget(self.newLabel)
mainLayout.addWidget(self.textbox2)
self.checkboxLayout("delHistory_CB", "Delete\nHistory", "freezeTrans_CB", "Freeze\nTransforms", mainLayout, False)
self.buttonLayout = QtGui.QGridLayout()
mainLayout.addLayout(self.buttonLayout)
self.buttonLayout.addWidget(self.cancelButton, 0, 0)
self.buttonLayout.addWidget(self.okButton, 0, 1)
self.setLayout(mainLayout)
我的问题是,当我尝试将信号连接到它时,它不起作用。到目前为止我看过的所有教程都只将信号连接到不是通过在另一个方法中调用方法创建的小部件(我意识到这可能不是正确的术语,但就像我说的,我是新手:( ) 我将 post 我编写的代码尝试连接下面的信号。我的猜测是我必须指定创建复选框的方法,但我也无法让它工作.那么我如何连接这个信号呢?也请随时纠正我的术语:)感谢任何能提供帮助的人:)
def createConnections(self):
self.selection_CB.toggled.connect(self.checkboxLine1_ChangeState)
您在哪里以及如何设置变量 self.selection_CB
?
在您的 checkboxLayout
函数中,您可以为您的复选框添加一个 return
,如下所示:
`return [name1, name2]`
然后在调用函数时简单地分配它们并从那里连接事件:
self.check1, self.check2 = self.checkboxLayout("selection_CB", "Selection", "name_CB", "Name", mainLayout, True)
或者如果它们总是连接到相同的函数,那么为什么不直接从 checkboxLayout
进行连接呢?
name1.stateChanged.connect(self.checkboxLine1_ChangeState)
我对编程还很陌生,所以请耐心等待。我正在尝试在 Autodesk Maya 中创建一个简单的脚本。我创建了一个并排设置两个复选框的方法(见下文)...
def checkboxLayout(self, name1, text1, name2, text2, parentLayout, initState):
##create and add horizontal layout
layout = QtGui.QHBoxLayout()
parentLayout.addLayout(layout)
width = 75
name1 = QtGui.QCheckBox(text1)
layout.addWidget(name1)
name1.setMinimumWidth(width)
name1.setMaximumWidth(width)
name1.setChecked(initState)
name2 = QtGui.QCheckBox(text2)
layout.addWidget(name2)
name2.setMinimumWidth(width)
name2.setMaximumWidth(width)
后来我调用了这个方法两次,以免我不得不将相同的大代码块写出两次...
def createLayout(self):
##Layout
mainLayout = QtGui.QVBoxLayout()
mainLayout.addWidget(self.title)
mainLayout.addSpacerItem(self.titleSpacer)
mainLayout.addWidget(self.oldLabel)
self.checkboxLayout("selection_CB", "Selection", "name_CB", "Name", mainLayout, True)
mainLayout.addWidget(self.textbox1)
mainLayout.addSpacerItem(self.midSpacer)
mainLayout.addWidget(self.newLabel)
mainLayout.addWidget(self.textbox2)
self.checkboxLayout("delHistory_CB", "Delete\nHistory", "freezeTrans_CB", "Freeze\nTransforms", mainLayout, False)
self.buttonLayout = QtGui.QGridLayout()
mainLayout.addLayout(self.buttonLayout)
self.buttonLayout.addWidget(self.cancelButton, 0, 0)
self.buttonLayout.addWidget(self.okButton, 0, 1)
self.setLayout(mainLayout)
我的问题是,当我尝试将信号连接到它时,它不起作用。到目前为止我看过的所有教程都只将信号连接到不是通过在另一个方法中调用方法创建的小部件(我意识到这可能不是正确的术语,但就像我说的,我是新手:( ) 我将 post 我编写的代码尝试连接下面的信号。我的猜测是我必须指定创建复选框的方法,但我也无法让它工作.那么我如何连接这个信号呢?也请随时纠正我的术语:)感谢任何能提供帮助的人:)
def createConnections(self):
self.selection_CB.toggled.connect(self.checkboxLine1_ChangeState)
您在哪里以及如何设置变量 self.selection_CB
?
在您的 checkboxLayout
函数中,您可以为您的复选框添加一个 return
,如下所示:
`return [name1, name2]`
然后在调用函数时简单地分配它们并从那里连接事件:
self.check1, self.check2 = self.checkboxLayout("selection_CB", "Selection", "name_CB", "Name", mainLayout, True)
或者如果它们总是连接到相同的函数,那么为什么不直接从 checkboxLayout
进行连接呢?
name1.stateChanged.connect(self.checkboxLine1_ChangeState)