How to connect custom signal to slot in pyside with the new syntax?

前端 未结 1 1973
逝去的感伤
逝去的感伤 2021-01-15 12:16

This is an example from an video tutorial:

#!/usr/bin/env python3

import sys
from PySide.QtCore import *
from PySide.QtGui import *

class ZeroSpinBox(QSpin         


        
相关标签:
1条回答
  • 2021-01-15 12:43

    Your have to declare new signal in class your implemented or inheritance;

    class ZeroSpinBox (QSpinBox):
        atzero = Signal(int)
        .
        .
    

    Then, your can call it in new-style signal. For emit signal;

            self.emit(SIGNAL("atzero(int)"), self.zeros)
    

    Change to

            self.atzero.emit(self.zeros)
    

    For connect signal;

             self.connect(zerospinbox, SIGNAL("atzero(int)"), self.announce)
    

    Change to

             zerospinbox.atzero.connect(self.announce)
    

    Also you can read this document to more information.


    Implement code example (PyQt4 also same PySide, different is name Signal & pyqtSignal);

    import sys
    from PyQt4.QtCore import *
    from PyQt4.QtGui import *
    
    class ZeroSpinBox(QSpinBox):
        atzero = pyqtSignal(int)
    
        zeros = 0
    
        def __init__(self):
            super(ZeroSpinBox, self).__init__()
            self.valueChanged.connect(self.checkzero)
    
        def checkzero(self):
            if self.value() == 0:
                self.zeros += 1
    #             self.emit(SIGNAL("atzero(int)"), self.zeros)
                self.atzero.emit(self.zeros)
    
    
    
    class Form(QDialog):
    
        def __init__(self):
            super(Form, self).__init__()
    
            dial = QDial()
            dial.setNotchesVisible(True)
            zerospinbox = ZeroSpinBox()
            layout = QHBoxLayout()
            layout.addWidget(dial)
            layout.addWidget(zerospinbox)
            self.setLayout(layout)
    
            dial.valueChanged.connect(zerospinbox.setValue)
            zerospinbox.valueChanged.connect(dial.setValue)
            zerospinbox.atzero.connect(self.announce)
    #         self.connect(zerospinbox, SIGNAL("atzero(int)"), self.announce)
    
            self.setWindowTitle("Signals")
    
        def announce(self, zeros):
            print("zerospinbox has been at zero " + str(zeros) + " times.")
    
    
    
    app = QApplication(sys.argv)
    form = Form()
    form.show()
    app.exec_()
    
    0 讨论(0)
提交回复
热议问题