![](/img/trans.png)
[英]How to add Widgets imported from another module to QMainWindow in PySide2
[英]How to catch a Signal from QMainWindow class by another thread on PySide?
我有一個MainWindow類,該類上運行有Gui應用程序,我希望每次我從應用程序中單擊按鈕時,都會發出信號並被另一個線程捕獲。 有我的示例代碼(很抱歉,我沒有發布我的真實代碼,但現在確實很大):
from PySide.QtGui import *
from PySide.QtCore import *
import sys
import mainGui #Gui file
class MainWindow(QMainWindow, mainGui.Ui_MainWindow):
mySignal = Signal()
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.newThread = workThread()
self.newThread.start()
#myButton is part of Gui application
self.myButton.clicked.connect(self.myfunction)
def myfunction(self):
self.mySignal.emit()
(...) #Other functions and methods
class workThread(QThread):
def __init__(self, parent=None):
super(workThread, self).__init__(parent)
#The problem:
MainWindow.mySignal.connect(self.printMessage)
def run(self):
(...)
def printMessage(self):
print("Signal Recived")
(...)
def main():
app = QApplication(sys.argv)
form = MainWindow()
form.show()
app.exec_()
if __name__=="__main__":
main()
...並且出現以下錯誤:MainWindow.mySignal.connect(self.printMessage)AttributeError:'PySide.QtCore.Signal'對象沒有屬性'connect'
有什么想法我該如何解決? 提前致謝!
信號就像方法一樣-它們必須綁定到實例。 如果您嘗試直接通過類訪問它們,它們將無法正常工作。
解決該示例的一種方法是將MainWindow
的實例作為線程的父級傳入,如下所示:
self.newThread = workThread(self)
...
parent.mySignal.connect(self.printMessage)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.