修改下问题,之前的代码过于冗杂。先将测试代码贴在这里。
在该测试代码中,当这两句以如下方式排列时,voidSlot无法获得sender。但是当将这两句顺序调换,则两个槽函数均能获得sender()。
stageType.currentTextChanged[str].connect(self.loadStage)
connectBut.clicked.connect(self.voidSlot)
import sys,PySide6
from PySide6.QtWidgets import QApplication,QPushButton, QComboBox,QWidget,QVBoxLayout,QHBoxLayout
class Tab(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
stageType = QComboBox(self)
stageType.setObjectName('stageType')
stageType.addItem('阶段类型')
stageType.addItem('阶段类型1')
stageType.addItem('阶段类型2')
connectBut = QPushButton('连接',self)
connectBut.setObjectName('Connect ')
stageType.currentTextChanged[str].connect(self.loadStage)
connectBut.clicked.connect(self.voidSlot)
layout = QVBoxLayout(self)
selectLayout = QHBoxLayout()
selectLayout.addWidget(stageType)
controlLayout =QHBoxLayout()
controlLayout.addWidget(connectBut)
layout.addLayout(selectLayout)
layout.addLayout(controlLayout)
self.setLayout(layout)
@PySide6.QtCore.Slot()
def loadStage(self,stage_type):
print('Load Stage'+str(self.sender()))
@PySide6.QtCore.Slot()
def voidSlot(self):
print('voidSlot'+str(self.sender()))
if __name__ == '__main__':
app = QApplication(sys.argv)
tab = Tab()
tab.show()
sys.exit(app.exec_())
------下方为旧提问,可以无视---------
我一个PyQT初学者,最近在写这段代码时遇到,由connect连接的信号与槽,当信号被触发,进入槽的时候,无法获取sender()
其中关于这行代码 stageType.currentTextChanged[str].connect(self.loadStage)
,当改代码位于当前位置(见下部分代码),会导致后续的connectBut.clicked.connect(self.voidSlot)
,startBut.clicked.connect(self.voidSlot)
,cancelBut.clicked.connect(self.voidSlot)
中在,voidSlot
槽中无法获取sender()
。返回值为None
但是当这句话stageType.currentTextChanged[str].connect(self.loadStage)
位于上述三句话后时,连接的loadStage
槽与voidSlot
槽都能获取到sender()
。
但是在我的程序中,该段代码需要执行N次,因此如果放在上述三句话后,当执行第二次时,在第二个窗口中,voidSlot
依旧无法获得sender()
当前环境python=3.9 qt为Pyside6.2.1。
如果将环境变为pyqt5.15.6时,不会出现上述问题。
import sys,PySide6
from PySide6.QtWidgets import QApplication,\
QPushButton, QTabWidget, QComboBox, QLineEdit, QLabel
# 省略
class Tab(QTabWidget):
'''标签页'''
def __init__(self,statusBar,parent):
pass
# 省略
def initUI(self, tab, name):
'''
初始化一个标签页
'''
stageType = QComboBox(tab)
stageType.setObjectName('stageType')
stageType.currentTextChanged[str].connect(self.loadStage)
stageName = QComboBox(tab)
stageName.setObjectName('stageName')
stageNowLizhi = QLineEdit(tab)
stageNowLizhi.setObjectName('stageNowLizhi')
defaultStage = QPushButton('默认关卡',tab)
nowStatus = QLabel("这里用来显示现在的状态",tab)
connectBut = QPushButton('连接',tab)
connectBut.setObjectName('Connect '+name)
connectBut.clicked.connect(self.voidSlot)
startBut = QPushButton('开始行动',tab)
startBut.setObjectName('Start '+name)
startBut.clicked.connect(self.voidSlot)
cancelBut = QPushButton('停止行动',tab)
cancelBut.setObjectName('Cancel '+name)
cancelBut.clicked.connect(self.voidSlot).
# 省略
#以下为两个槽的函数
@PySide6.QtCore.Slot()
def loadStage(self,stage_type):
'''加载关卡数据'''
stageName = self.sender().parent().findChild(QComboBox,"stageName")
stageName.clear()
for line in self.stage_data:
if line[4] == stage_type:
stageName.addItem(str(line[0]))
@PySide6.QtCore.Slot()
def voidSlot(self):
'''默认用于显示哪个按钮被点击的槽'''
sender = self.sender()
self.statusBar.showMessage(sender.objectName()+' been clicked!',5000)