如何防止PyQt按钮的点击信号在槽函数有装饰器时发送布尔值?

huangapple go评论94阅读模式
英文:

How to prevent the pyqt button's clicked signal from sending Boolean value when slot functions have a decorator?

问题

我正在使用Qt Designer和PyQt5构建一个QT项目。我有一个按钮,它发送一个带有布尔值的信号。

绑定信号和函数的代码如下(我的装饰器在类内部,但装饰器不应该是问题,因为不管使用哪个装饰器,单击信号都会传递布尔参数):

  1. class Demo:
  2. def __init__(self):
  3. # 绑定信号和函数
  4. ...
  5. self.ui.pushButton_disconnect.clicked.connect(
  6. self.robot.disconnect_devices
  7. )
  8. ...
  9. def my_decorator(func):
  10. def wrapper(self, *args, **kwargs):
  11. # args和kwargs是必需的,因为其他装饰的函数有参数
  12. # 做一些操作
  13. return func(self, *args, **kwargs)
  14. return wrapper
  15. # 函数
  16. @my_decorator
  17. def disconnect_devices(self, *args):
  18. print(args)
  19. # 做一些操作

现在我必须使用*args来接收False。我知道在connect中使用lambda也可以避免这个问题。

我已经找到了原因:我正在使用装饰器装饰函数disconnect_devices,似乎点击信号会自动传递参数,因为装饰器中使用了*args,所以我必须在我的函数中使用*args。有没有办法避免这个问题?或者我是否可以将clicked信号修复为不带参数的信号?

英文:

I am building a QT project with qt designer and pyqt5. I have one button that sends a signal with a boolean value.
My code to bind the signal and function(my decorator is inside the class, but it shouldn't be a problem with my decorator, because no matter what decorator is used, the clicked signal will pass in the bool parameter):

  1. class Demo:
  2. def __init__(self):
  3. # bind the signal and function
  4. ...
  5. self.ui.pushButton_disconnect.clicked.connect(
  6. self.robot.disconnect_devices
  7. )
  8. ...
  9. def my_decorator(func):
  10. def wrapper(self,*args,**kwargs):
  11. '''
  12. args and kwargs is necessary
  13. because other decorated functions have parameters
  14. '''
  15. # do something
  16. return func(self,*args,**kwargs)
  17. retrun wrapper
  18. # function
  19. @my_decorator
  20. def disconnect_devices(self, *args):
  21. print(args)
  22. # do something

Now I have to use *args to receive False. I know that using lambda in connect can also avoid this issue.
I have found the reason: I am using a decorator to decorate the function disconnect_devices, it seems that clicked signal will automatically pass in parameters because *args is used in the decorator, so I have to use *args to in my function. Is there a way to avoid this? Or can I fix the clicked signal as a signal without parameters?

答案1

得分: 1

你不能修改QAbstractButton::clicked信号的签名。

相反,你可以创建一个新的装饰器来包装原始的装饰器,像这样。

  1. ...
  2. def my_decorator(func):
  3. def wrapper(self, *args, **kwargs):
  4. return func(self, *args, **kwargs)
  5. return wrapper
  6. def my_decorator_ignoring_args(func):
  7. return my_decorator(lambda self, *_: func(self))
  8. class Demo:
  9. ...
  10. @my_decorator_ignoring_args
  11. def disconnect_devices(self):
  12. ...
  13. ...

另外,你可以只定义一个装饰器,如果被装饰的函数没有参数,就包装它,像这样。

  1. import inspect
  2. ...
  3. def my_decorator(func):
  4. if len(inspect.signature(func).parameters) == 1:
  5. original_func = func
  6. func = lambda self, *_: original_func(self)
  7. def wrapper(self, *args, **kwargs):
  8. return func(self, *args, **kwargs)
  9. return wrapper
  10. class Demo:
  11. ...
  12. @my_decorator
  13. def disconnect_devices(self):
  14. ...
英文:

You can't modify the signature of the QAbstractButton::clicked signal.

Instead, you can create a new decorator wrapping the original decorator like this.

  1. ...
  2. def my_decorator(func):
  3. def wrapper(self, *args, **kwargs):
  4. return func(self, *args, **kwargs)
  5. return wrapper
  6. def my_decorator_ignoring_args(func):
  7. return my_decorator(lambda self, *_: func(self))
  8. class Demo:
  9. ...
  10. @my_decorator_ignoring_args
  11. def disconnect_devices(self):
  12. ...
  13. ...

Also, you can define only single decorator and wrap the decorated function if it has no parameters, like this.

  1. import inspect
  2. ...
  3. def my_decorator(func):
  4. if len(inspect.signature(func).parameters) == 1:
  5. original_func = func
  6. func = lambda self, *_: original_func(self)
  7. def wrapper(self, *args, **kwargs):
  8. return func(self, *args, **kwargs)
  9. return wrapper
  10. class Demo:
  11. ...
  12. @my_decorator
  13. def disconnect_devices(self):
  14. ...

huangapple
  • 本文由 发表于 2023年6月26日 16:52:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76555070.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定