gpt4 book ai didi

python - 如何将带有可变参数的多个 PyQt 信号连接到单个插槽/信号中继器

转载 作者:行者123 更新时间:2023-12-04 09:45:08 24 4
gpt4 key购买 nike

例子:

class MyClass(QObject):
signal_1 = pyqtSignal(str, int)
signal_2 = pyqtSignal(int, int)
signal_3 = pyqtSignal(str, int, int)

假设这些信号中的每一个都连接到其他地方以执行各种功能,但是,当发出任何信号时,我还希望执行特定的功能。这个不分青红皂白的函数所做的事情只关心最后一个 int随信号发出的参数。该插槽表面上看起来像:
class OtherClass(QObject):

...

@pyqtSlot(int)
def unifiedResponse(self, index):
# Here I would do something with that index

有没有办法将具有任意参数的任意数量的信号直接连接到插槽或中继器信号?

如果有一种方法可以像这样定义一个插槽:
@pyqtSlot(???)
def unifiedResponse(self, *args):
important_var = args[-1]

然后我可以简单地捕获最后一个参数。但是,我一直没有成功确定如何形成插槽签名。

更新:

我可能已经回答了我自己的问题,使用 lambda s:
signal_1.connect(lambda _, source: OtherClass.unifiedResponse(source))
signal_2.connect(lambda _, source: OtherClass.unifiedResponse(source))
signal_3.connect(lambda _, _, source: OtherClass.unifiedResponse(source))

然而,下面来自@eyllanesc 的解决方案是首选,因为它允许更大的灵活性,信号计数越大。

最佳答案

您必须通过几个 pyqtSlot 设置信号的所有签名:

from PyQt5.QtCore import pyqtSignal, pyqtSlot, QCoreApplication, QObject, QTimer


class MyClass(QObject):
signal_1 = pyqtSignal(str, int)
signal_2 = pyqtSignal(int, int)
signal_3 = pyqtSignal(str, int, int)


class OtherClass(QObject):
@pyqtSlot(str, int)
@pyqtSlot(int, int)
@pyqtSlot(str, int, int)
def unifiedResponse(self, *args):
print(args)


def main():
import sys

app = QCoreApplication(sys.argv)

sender = MyClass()
receiver = OtherClass()

sender.signal_1.connect(receiver.unifiedResponse)
sender.signal_2.connect(receiver.unifiedResponse)
sender.signal_3.connect(receiver.unifiedResponse)

def on_timeout():
sender.signal_1.emit("StackOverflow", 1)
sender.signal_2.emit(1, 2)
sender.signal_3.emit("StackOverflow", 1, 2)

QTimer.singleShot(1000, QCoreApplication.quit)

QTimer.singleShot(1000, on_timeout)

sys.exit(app.exec_())


if __name__ == "__main__":
main()

如果通常要发送多种类型的数据,那么最好使用更通用的数据类型,例如列表(或对象):
class MyClass(QObject):
signal_1 = pyqtSignal(list)
signal_2 = pyqtSignal(list)
signal_3 = pyqtSignal(list)


class OtherClass(QObject):
@pyqtSlot(list)
def unifiedResponse(self, args):
print(args)


def main():
import sys

app = QCoreApplication(sys.argv)

sender = MyClass()
receiver = OtherClass()

sender.signal_1.connect(receiver.unifiedResponse)
sender.signal_2.connect(receiver.unifiedResponse)
sender.signal_3.connect(receiver.unifiedResponse)

def on_timeout():
sender.signal_1.emit(["StackOverflow", 1])
sender.signal_2.emit([1, 2])
sender.signal_3.emit(["StackOverflow", 1, 2])

QTimer.singleShot(1000, QCoreApplication.quit)

QTimer.singleShot(1000, on_timeout)

sys.exit(app.exec_())

更新:

没有访问最后一个元素的优雅方法,但是第一个元素,因为槽签名必须是信号签名的子集,例如签名为“int”的槽可以接受具有第一个类型为“的签名的信号” int",其他参数被丢弃:
from PyQt5.QtCore import pyqtSignal, pyqtSlot, QCoreApplication, QObject, QTimer


class MyClass(QObject):
signal_1 = pyqtSignal(int, str)
signal_2 = pyqtSignal(int, int)
signal_3 = pyqtSignal(int, str, int)


class OtherClass(QObject):
@pyqtSlot(int)
def unifiedResponse(self, index):
print(index)


def main():
import sys

app = QCoreApplication(sys.argv)

sender = MyClass()
receiver = OtherClass()

sender.signal_1.connect(receiver.unifiedResponse)
sender.signal_2.connect(receiver.unifiedResponse)
sender.signal_3.connect(receiver.unifiedResponse)

def on_timeout():
sender.signal_1.emit(1, "StackOverflow")
sender.signal_2.emit(2, 2)
sender.signal_3.emit(3, "StackOverflow", 1)

QTimer.singleShot(1000, QCoreApplication.quit)

QTimer.singleShot(1000, on_timeout)

sys.exit(app.exec_())


if __name__ == "__main__":
main()

关于python - 如何将带有可变参数的多个 PyQt 信号连接到单个插槽/信号中继器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62163256/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com