mercredi 29 juin 2016

PyQt4 Signal and QObject.Emit()


I am learning GUI programing using python and pyqt4 and this is the app i am working on to learn Signal and Slots. This is a simple app which has a spinbox and a dialogue box that connected together using pyqt signals. but i added a class zerospinbox ,it supposed to print massage to console every time that the spinbox or dialogbox value becomes zero and count the number of occurrence of it using a, QObject.Emit() signal. I followed tutorial book to write it and whatever i do it does not show the massage.So if you can please take a look at the code and tell me where i am wrong:

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class Form(QDialog):

    def __init__(self, parent=None):
        super(Form, self).__init__(parent)

        dial = QDial()
        dial.setNotchesVisible(True)
        spinbox = QSpinBox()

        layout = QHBoxLayout()
        layout.addWidget(dial)
        layout.addWidget(spinbox)
        zerospinbox = ZeroSpinBox()
        self.setLayout(layout)

        self.connect(dial, SIGNAL("valueChanged(int)"),
                     spinbox,SLOT("setValue(int)"))
        self.connect(spinbox, SIGNAL("ValueChanged(int)"),
                     dial,SLOT("setValue(int)"))
        self.setWindowTitle("Signal and Slots")
        zerospinbox = ZeroSpinBox()

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

    def announce(self,zeros):
        print("ZeroSpinBox has been at zero %d times" % zeros)        

class ZeroSpinBox(QSpinBox):

    zeros=0


    def __init__(self, parent=None):
        super(ZeroSpinBox, self).__init__(parent)
        self.connect(self, SIGNAL("valueChanged(int)"), self.checkzero)



    def checkzero(self):
        if self.value()==0:
            self.zeros +=1
            self.emit(SIGNAL("atzero"),self.zeros)

app =QApplication(sys.argv)
form = Form()
form.show()
app.exec_()

thanks


Aucun commentaire:

Enregistrer un commentaire