嗯,我熟悉Qt,但是當使用PyQt時,信號/插槽的語法真的讓我困惑。 當使用C++/Qt時,編譯器會給你一個提示,你對信號/插槽的錯誤信息,但是PyQt的默認配置沒有提供有關錯誤的提示。有沒有一種方式或諸如調試觸發模式來使PyQt顯示更多信息? 代碼是如下:處理用戶定義信號/插槽時PyQt和Qt的區別
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import time
class workThread(QThread):
def __init__(self,parent = None):
super(workThread,self).__init__(parent)
self.mWorkDoneSignal = pyqtSignal() ## some people say this should be defined as clas member, however, I defined it as class member and still fails.
def run(self):
print "workThread start"
time.sleep(1)
print "workThread stop"
print self.emit(SIGNAL("mWorkDoneSignal"))
class MainWidget(QWidget):
def __init__(self , parent = None):
super(MainWidget,self).__init__(parent)
@pyqtSlot()
def display(self):
print "dispaly"
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
c = workThread()
d = MainWidget()
##In Qt, when using QObject::connect or such things, the return value will show the
## signal/slot binding is success or failed
print QObject.connect(c,SIGNAL('mWorkDoneSignal()'),d,SLOT('display()'))
c.start()
d.show()
app.exec_()
在C++中,QObject的::連接返回值將顯示信號/槽結合是成功與否。在PyQt中,返回值爲True,但不會觸發插槽。 我的問題: 1)信號應該是類成員還是實例成員? 2)如果QObject.connect的返回值無法給出綁定提示是否成功,還有其他方法可以檢測到它嗎? 我想綁定信號發送器和插槽接收器之外的信號/插槽,所以我傾向於使用QObject.connect方法。但是我怎麼能寫出這個正確的,我嘗試了以下方法,都失敗了。
QObject.connect(c,SIGNAL('mWorkDoneSignal'),d,SLOT('display'))
QObject.connect(c,SIGNAL('mWorkDoneSignal()'),d,SLOT('display()'))