2016-08-12 9 views
1

私はあなたが遭遇した以下の問題であなたの助けが必要です。 私は、PyQt5シグナルを使って通信する必要がある2つのPythonファイルMain.pyとModule.pyを持っています。ここでは、コードです:PyQt5シグナル通信エラー

Main.py

from PyQt5.QtCore import * 
from PyQt5.QtGui import * 
from PyQt5.QtWidgets import * 

from MyGUI import main_window_GUI 

from Modules import Module.py 

class MainWindow(QMainWindow, main_window_GUI.Ui_main_window): 
    def __init__(self): 
     QMainWindow.__init__(self) 
     main_window_GUI.Ui_main_window.__init__(self) 
     self.setupUI(self) 

     sub_win = QMdiSubWindow() 
     sub_win.setWidget(Module.moduleWindow()) 
     self.mdi.addSubWindow(sub_win) 

     # this part reports error saying: 
     # 'PyQt5.QtCore.pyqtSignal' object has no attribute 'connect' 
     Module.moduleWindow.my_signal.connect(self.do_something) 

    def do_something(self): 
     pass 

Module.py

from PyQt5.QtCore import * 
from PyQt5.QtGui import * 
from PyQt5.QtWidgets import * 

from MyGUI import module_window_GUI 

class moduleWindow(QMainWindow, module_window_GUI.Ui_module_window): 
    my_signal = pyqtSignal() 
    def __init__(self): 
     QMainWindow.__init__(self) 
     module_window_GUI.Ui_module_window.__init__(self) 
     self.setupUI(self) 

     # the rest is not important 

    # what is important is th following function 
    def closeEvent(self, event): 
     # when the user closes this subwindow signal needs to 
     # be emitted so that the MainWindow class knows that 
     # it's been closed. 
     self.my_signal.emit() 
     event.accept() 

は、ヘルプの任意の種類は歓迎以上です。前もって感謝します。

答えて

0

あなたはmoduleWindowクラスのインスタンスからではなく、クラス自体からの信号を接続する必要があります。

from PyQt5.QtCore import * 
from PyQt5.QtGui import * 
from PyQt5.QtWidgets import * 

from MyGUI import main_window_GUI 

from Modules import Module 

class MainWindow(QMainWindow, main_window_GUI.Ui_main_window): 
    def __init__(self): 
     QMainWindow.__init__(self) 
     main_window_GUI.Ui_main_window.__init__(self) 
     self.setupUI(self) 

     sub_win = QMdiSubWindow() 
     module_window = Module.moduleWindow() 
     sub_win.setWidget(module_window) 
     self.mdi.addSubWindow(sub_win) 

     module_window.my_signal.connect(self.do_something) 

    @pyqtSlot() 
    def do_something(self): 
     pass 

documentation

で報告されているように私も pyqtSlotでdo_something方法を飾るために推薦します
+0

私はこれを試しましたが、今度はエラーは発生しませんが、シグナルが送出されたときにdo_something関数が実行されていません。助言がありますか? Danieleに感謝します。 – Beller0ph0n

+0

は 'closeEvent'が実行されていますか? –

+0

また、 'moduleWindow'のインスタンスへの参照を保持する必要があります。 – ekhumoro

関連する問題