使用新式语法连接重载的 PyQT 信号 [英] Connecting an overloaded PyQT signal using new-style syntax

查看:43
本文介绍了使用新式语法连接重载的 PyQT 信号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计一个自定义小部件,它基本上是一个 QGroupBox,其中包含可配置数量的 QCheckBox 按钮,其中每个按钮都应控制表示的位掩码中的特定位通过 QBitArray.为了做到这一点,我将 QCheckBox 实例添加到了 QButtonGroup,每个按钮都有一个整数 ID:

I am designing a custom widget which is basically a QGroupBox holding a configurable number of QCheckBox buttons, where each one of them should control a particular bit in a bitmask represented by a QBitArray. In order to do that, I added the QCheckBox instances to a QButtonGroup, with each button given an integer ID:

    def populate(self, num_bits, parent = None):
        """
        Adds check boxes to the GroupBox according to the bitmask size
        """
        self.bitArray.resize(num_bits)
        layout = QHBoxLayout()

        for i in range(num_bits):
            cb = QCheckBox()
            cb.setText(QString.number(i))
            self.buttonGroup.addButton(cb, i)
            layout.addWidget(cb)
        self.setLayout(layout)

然后,每次用户点击包含在 self.buttonGroup 中的复选框时,我希望 self.bitArray 得到通知,以便在可以相应地设置/取消设置数组.为此,我打算将 QButtonGroup 的 buttonClicked(int) 信号连接到 QBitArray 的 toggleBit(int) 方法,并且,尽可能pythonic,我想使用新式信号语法,所以我尝试了这个:

Then, each time a user would click on a checkbox contained in self.buttonGroup, I'd like self.bitArray to be notified so the corresponding bit in the array can be set/unset accordingly. For that I intended to connect QButtonGroup's buttonClicked(int) signal to QBitArray's toggleBit(int) method and, to be as pythonic as possible, I wanted to use new-style signals syntax, so I tried this:

self.buttonGroup.buttonClicked.connect(self.bitArray.toggleBit)

问题在于 buttonClicked 是一个过载信号,所以还有 buttonClicked(QAbstractButton*) 签名.事实上,当程序正在执行时,当我单击复选框时会出现此错误:

The problem is that buttonClicked is an overloaded signal, so there is also the buttonClicked(QAbstractButton*) signature. In fact, when the program is executing I get this error when I click a check box:

The debugged program raised the exception unhandled TypeError
"QBitArray.toggleBit(int): argument 1 has unexpected type 'QCheckBox'"

这清楚地显示了 toggleBit 方法收到了 buttonClicked(QAbstractButton*) 信号,而不是 buttonClicked(int) 信号.

which clearly shows the toggleBit method received the buttonClicked(QAbstractButton*) signal instead of the buttonClicked(int) one.

那么,问题是,如何指定信号连接,使用新式语法,以便 self.bitArray 接收 buttonClicked(int) 信号而不是默认的重载 - buttonClicked(QAbstractButton*)?

So, the question is, how can I specify the signal connection, using new-style syntax, so that self.bitArray receives the buttonClicked(int) signal instead of the default overload - buttonClicked(QAbstractButton*)?

PyQT 的新型信号和插槽支持文档 指出您可以使用 pyqtSlot 装饰器来指定要连接到给定插槽的信号,但这是针对您的插槽的正在创造.当插槽来自现成"类时该怎么办?是唯一的选择子类化它然后用正确的装饰器重新实现给定的插槽吗?

The PyQT's New-style Signal and Slot Support documentation states you can use pyqtSlot decorators to specify which signal you want to connect to a given slot, but that is for a slot you are creating. What to do when the slot is from a "ready made" class? Is the only option subclassing it and then reimplementing the given slot with the right decorator?

推荐答案

在浏览相关问题时我发现了这个答案正是我所需要的.仅将 QButtonGroup 的 buttonClicked(int) 信号连接到 QBitArray 的 toggleBit(int) 的正确新型信号语法,忽略其他重载签名,涉及在括号中指定所需的类型,如下所示:

While browsing for related questions I found this answer to be exactly what I needed. The correct new-style signal syntax for connecting only QButtonGroup's buttonClicked(int) signal to QBitArray's toggleBit(int), ignoring the other overloaded signatures, involves specifying the desired type in brackets, like this:

self.buttonGroup.buttonClicked[int].connect(self.bitArray.toggleBit)

这篇关于使用新式语法连接重载的 PyQT 信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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