如何使用新语法将自定义信号连接到 pyside 中的插槽? [英] How to connect custom signal to slot in pyside with the new syntax?

查看:117
本文介绍了如何使用新语法将自定义信号连接到 pyside 中的插槽?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是视频教程中的示例:

#!/usr/bin/env python3

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

class ZeroSpinBox(QSpinBox):

    zeros = 0

    def __init__(self):
        super().__init__()
        self.valueChanged.connect(self.checkzero)

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



class Form(QDialog):

    def __init__(self):
        super().__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_()

请注意,我为 zerospinbox 对象建立了两个连接,第一个使用新的连接语法,第二个使用旧的语法.还有一行用于与新语法建立相同的第二个连接,但它不起作用,因此将其注释掉.atzero 信号是定制的,看起来这个信号在新语法中不能很好地工作.这是排放方式的原因吗?我如何将新语法也应用于 atzero 信号?

Note that I have made two connections for the zerospinbox object, the first one with the new connect syntax, the second one with the old syntax. There is also a line for making the same second connection with the new syntax, but it doesn't work, hence it's commented out. The atzero signal is custom made, and it seems signals made this does not work well with the new syntax. Is this due to the methods of emission? How can I applied the new syntax to the atzero signal as well?

推荐答案

您必须在实现或继承的类中声明新信号;

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)

改为

        self.atzero.emit(self.zeros)

用于连接信号;

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

改为

         zerospinbox.atzero.connect(self.announce)

您也可以阅读此文档了解更多信息.

Also you can read this document to more information.

实现代码示例(PyQt4也是同一个PySide,不同的是名字Signal & pyqtSignal);

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_()

这篇关于如何使用新语法将自定义信号连接到 pyside 中的插槽?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆