简体   繁体   中英

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

You created three spinboxes, but only added the first of them to the dialog layout. The only one that is shown is a QSpinBox , not a ZeroSpinBox . The other two just get deleted without being shown.

You used old-style signal/slot syntax and made a spelling error, so the connection did not work. Do not use this obsolete old-style syntax. It is very error-prone and does not raise an exception when it fails. Always use the new-style syntax:

        dial.valueChanged[int].connect(spinbox.setValue)
        spinbox.valueChanged[int].connect(dial.setValue)

You created a spinbox subclass which is not really needed. It would be much simpler to do everything within the main class:

        spinbox.valueChanged[int].connect(self.announce)
        self.zeros = 0

    def announce(self, value):
        if value == 0:
            self.zeros += 1
            print("SpinBox has been at zero %d times" % self.zeros)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM