2015-12-30 117 views
1

我想知道有人能幫我解決這個問題,關於PyQt5中的插槽連接。下面的代碼片段會告訴你我的問題是什麼。MainWindow對象沒有'連接'屬性

class MainWindow(QMainWindow): 
    def __init__(self): 
     super(MainWindow, self).__init__() 
     path = os.path.join(os.path.dirname(__file__), 'GUI/Main_GUI.ui') 
     self.gui = loadUi(path) 

     self.gui.button_1.clicked.connect(self.run.this) 

    def _connect_my_slots(self, origin): 
     self.connect(origin, SIGNAL('completed'), self._show_results) 
     self.connect(origin, SIGNAL('error'), self._show_error) 

    def run_this(self): 
     myThread = LongRunningThing() 
     self._connect_my_slots(self.myThread) # THIS IS THE PART THAT CAUSES ERROR 

正如你可以看到我的MainWindow對象是我的UI文件(從QtDesigner 5)有一次我打電話_connect_my_slots功能,它拋出一個錯誤:

AttributError: 'MainWindow' object has no attribute 'connect'

回答

1

您使用的是舊式信號和槽,這在PyQt5中不再支持。

舊風格:

self.connect(origin, SIGNAL('completed'), self._show_results) 

現在應該寫在新的風格:

origin.completed.connect(self._show_results) 

有關詳細信息,請參閱New-style Signal and Slot Support的文檔。

+0

感謝很多tmoreau老式信號是問題的原因。爲了確保我在正確的軌道上,請告訴我,如果我的EMIT聲明也是舊式: self.emit(SIGNAL('completed'),self.result) 。 – Beller0ph0n

+0

是的,它是舊式的。 「SIGNAL」是你的線索。你需要知道的所有信號都在鏈接的文檔頁面上(即使是PyQt4,它也應該可以工作) – Mel

+0

再次感謝tmoreau。我已經閱讀過這份文件。非常感謝您的幫助! – Beller0ph0n