PyQt5 的 signal 與 slot 有所改變,例如,先定義一個 ZeroSignal 類: 使用時,一是綁定 slot 如下: 然後是找個機會發動之: 大約如此,完整代碼如下: OK! ...
PyQt5 的 signal 與 slot 有所改變,例如,先定義一個 ZeroSignal 類:
class ZeroSignal(QObject): atzero = pyqtSignal(int)
使用時,一是綁定 slot 如下:
self.zerosig = ZeroSignal()
self.zerosig.atzero[int].connect(self.countZero)
然後是找個機會發動之:
def checkZero(self): if self.value() == 0: self.zerosig.atzero.emit(self.value())
大約如此,完整代碼如下:
import os import sys from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * class ZeroSignal(QObject): atzero = pyqtSignal(int) class ZeroSpinBox(QSpinBox): def __init__(self, parent=None): super(ZeroSpinBox, self).__init__(parent) self.zeros = 0 self.valueChanged[int].connect(self.checkZero) self.zerosig = ZeroSignal() self.zerosig.atzero[int].connect(self.countZero) def countZero(self, v): if v == 0: self.zeros += 1 print(self.zeros) def checkZero(self): if self.value() == 0: self.zerosig.atzero.emit(self.value()) class Form(QDialog): def __init__(self, parent=None): super(Form, self).__init__(parent) dial = QDial() dial.setNotchesVisible(True) spin = ZeroSpinBox() layout = QHBoxLayout() layout.addWidget(dial) layout.addWidget(spin) self.setLayout(layout) dial.valueChanged.connect(spin.setValue) spin.valueChanged.connect(dial.setValue) spin.valueChanged.connect(self.emitZero) self.setWindowTitle("Sinal and Solt") self.zerobox = spin self.zerobox.zerosig.atzero[int].connect(self.annouce) def emitZero(self, v): if v == 0: self.zerobox.zerosig.atzero.emit(self.zerobox.zeros) def annouce(self, v): print("zero count: %d" % v) # print two times because add ZeroSpinBox emit once def run(): app = QApplication(sys.argv) form = Form() form.show() app.exec_() if __name__ == '__main__': run()
OK!