signal -> emit -> slotpython
signal.connect(slot)app
signal.disconnect(slot)函數
信號 (singal) 能夠鏈接無數多個槽 (slot),或者沒有鏈接槽也沒有問題,信號也能夠鏈接其餘的信號。 ui
鏈接的基本語句形式以下: who.singal.connect(slot) spa
信號是 QObject 的一個屬性。只有經過 connect方法鏈接起來,信號-槽機制就創建起來了。相似的信號還有 disconnect 方法和emit 方法。 code
disconnect 就是斷開信號-槽機制,而 emit 就是激活信號。
信號都是類的一個屬性,新的信號必須繼承自 QObject,而後由 PyQt5.QtCore.pyqtSingal(在 pyqt4 下是 PyQt4.QtCore.pyqtSingal)方法建立,這個方法接受的參數中最重要的是 types 類型,好比 int, bool 之類的,你能夠認爲這是信號傳遞的參數類型,但實際傳遞這些參數值的是 emit 方法。而後槽實際上就是通過特殊封裝的函數,這些函數固然須要接受一些參數或者不接受參數,而這些參數具體的值傳進來的是由 emit 方法執行的,而後咱們經過 who.singal.connect(slot) 這樣的形式將某個信號和某個槽鏈接起來, who 的信號,而後信號類自帶的鏈接方法,而後鏈接到 slot 某個函數上,在這裏隱藏的一個重要細節就是 emit 方法,好比說你定義一個新的信號,須要將點擊屏幕的具體 x,y 座標發送出去,內置的信號-槽將這一機制都完成了,若是你本身定義的信號和槽的話,好比 pyqtSingal(int,int) ,發送給 func(x,y),具體 x 和 y 的值你須要經過 emit(x,y) 來發送。至於何時發送,已經發送的 x,y 值的獲取,這應該又是另一個信號-槽機制的細節。
blog
from PyQt5.QtCore import * from PyQt5.QtGui import * class FindDialog(QDialog): findNext = pyqtSignal(str, Qt.CaseSensitivity) findPrevious = pyqtSignal(str, Qt.CaseSensitivity)
class FindDialog(QDialog): ......
@pyqtSlot() def findClicked(self): text = self.lineEdit.text() if self.caseCheckBox.isChecked(): cs = Qt.CaseSensitive else: cs = Qt.CaseInsensitive if self.backwardCheckBox.isChecked(): self.findPrevious.emit(text, cs) else: self.findNext.emit(text, cs)
class FindDialog(QDialog): ...... @pyqtSlot() def findClicked(self): ...... if self.backwardCheckBox.isChecked(): self.findPrevious.emit(text, cs) else: self.findNext.emit(text, cs)
#!/usr/bin/env python # -*- coding:utf-8 -*- ''' 查找對話框示例, 自定義信號槽 ''' from PyQt5.QtWidgets import * from PyQt5.QtCore import * from PyQt5.QtGui import * class FindDialog(QDialog): findNext = pyqtSignal(str, Qt.CaseSensitivity) findPrevious = pyqtSignal(str, Qt.CaseSensitivity) def __init__(self, parent = None): super().__init__(parent) label = QLabel(self.tr('Find &what:')) self.lineEdit = QLineEdit() label.setBuddy(self.lineEdit) self.caseCheckBox = QCheckBox(self.tr('Match &case')) self.backwardCheckBox = QCheckBox(self.tr('Search &backward')) self.findButton = QPushButton(self.tr('&Find')) self.findButton.setDefault(True) self.findButton.setEnabled(False) closeButton = QPushButton(self.tr('Close')) self.lineEdit.textChanged.connect(self.enableFindButton) self.findButton.clicked.connect(self.findClicked) closeButton.clicked.connect(self.close) topLeftLayout = QHBoxLayout() topLeftLayout.addWidget(label) topLeftLayout.addWidget(self.lineEdit) leftLayout = QVBoxLayout() leftLayout.addLayout(topLeftLayout) leftLayout.addWidget(self.caseCheckBox) leftLayout.addWidget(self.backwardCheckBox) rightLayout = QVBoxLayout() rightLayout.addWidget(self.findButton) rightLayout.addWidget(closeButton) rightLayout.addStretch() mainLayout = QHBoxLayout() mainLayout.addLayout(leftLayout) mainLayout.addLayout(rightLayout) self.setLayout(mainLayout) self.setWindowTitle(self.tr('Find')) self.setFixedHeight(self.sizeHint().height()) def enableFindButton(self, text): self.findButton.setEnabled(bool(text)) @pyqtSlot() def findClicked(self): text = self.lineEdit.text() if self.caseCheckBox.isChecked(): cs = Qt.CaseSensitive else: cs = Qt.CaseInsensitive if self.backwardCheckBox.isChecked(): self.findPrevious.emit(text, cs) else: self.findNext.emit(text, cs) if __name__ == '__main__': import sys app = QApplication(sys.argv) findDialog = FindDialog() def find(text, cs): print('find:', text, 'cs', cs) def findp(text, cs): print('findp:', text, 'cs', cs) findDialog.findNext.connect(find) findDialog.findPrevious.connect(findp) findDialog.show() sys.exit(app.exec_())
d繼承