信号源已被删除
在我的pyside6项目中,以下代码可以工作:
# in main.py
if __name__ == "__main__":
app = QApplication(sys.argv)
window = mainWindow.MainWindow()
controller = control.Controller(window)
window.show()
sys.exit(app.exec())
# in control.py
class Controller(QObject):
def __init__(self, main_window):
self.main_window = main_window
self.main_window.ui.action_Open.triggered.connect(lambda: file.open_file(self.main_window, self.target_data, self.file_target))
self.main_window.fileOpenDoneSignal.connect(self.setup_model_view)
def setup_model_view(self):
#setting up the model and view from the data in the new file
# in mainWindow.py
class MainWindow(QMainWindow):
fileOpenDoneSignal = Signal()
def __init__(self):
super().__init__()
self.ui = ui_mainWindow.Ui_MainWindow()
self.ui.setupUi(self)
# in file.py
def open_file(caller_obj, df, target):
# some procedure to open a new file
caller_obj.fileOpenDoneSignal.emit()
不过如果我想把信号移动到控制器层级:
# in control.py
class Controller(QObject):
fileOpenDoneSignal = Signal()
def __init__(self, main_window):
self.main_window = main_window
self.main_window.ui.action_Open.triggered.connect(lambda: file.open_file(self, self.target_data, self.file_target))
self.fileOpenDoneSignal.connect(self.setup_model_view)
def setup_model_view(self):
#setting up the model and view from the data in the new file
# in mainWindow.py
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.ui = ui_mainWindow.Ui_MainWindow()
self.ui.setupUi(self)
这将不起作用,错误:Signal source has been deleted
能否告诉我发生了什么?现在位于 file.open_file 的信号源难道不应该在控制器作用域内运行吗?到底删掉了什么?
我在考虑把所有信号和控件都移到控制器中,是否有合适的方法可以把信号移动到控制器?还是应该把它保留在 MainWindow 对象中?
我正在使用pyside6 6.11.1,是否仍然是BUG: PySide6 6.9+ “Signal source has been deleted” #24825 https://github.com/spyder-ide/spyder/issues/24825 中提到的那个错误?
解决方案
感谢musicamante和 ekhumoro,控制器类上缺少的 super().__init__() 是问题所在。现在运行正常。
musicamante,感谢指出我应该以更恰当的方式提问。下面给出一个最小可复现的代码,控制器中的信号可以正常工作:
# main.py
import sys
from PySide6.QtWidgets import QApplication
from test_signal import mainWindow, control
if __name__ == "__main__":
app = QApplication(sys.argv)
window = mainWindow.MainWindow()
controller = control.Controller(window)
window.show()
sys.exit(app.exec())
# mainWindow.py
from PySide6.QtWidgets import QMainWindow
from PySide6.QtGui import QAction
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.resize(600, 400)
menu_bar = self.menuBar()
file_menu = menu_bar.addMenu("&File")
self.open_action = QAction("&Open...", self)
file_menu.addAction(self.open_action)
# control.py
from PySide6.QtCore import QObject, Signal
from test_signal import file
class Controller(QObject):
fileOpenDoneSignal = Signal()
def __init__(self, main_window):
super().__init__()
self.main_window = main_window
self.main_window.open_action.triggered.connect(lambda: file.open_file(self))
self.fileOpenDoneSignal.connect(self.display_file)
def display_file(self):
print("Signal received")
# file.py
def open_file(caller):
caller.fileOpenDoneSignal.emit()
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。