鱼C论坛

 找回密码
 立即注册
查看: 16624|回复: 6

关于pyqt5中信号定义的问题

[复制链接]
发表于 2015-4-17 13:37:02 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
更新pyqt那个贴子遇到的一个小问题,得知正确答案后我会写在贴子里的~~
两段代码的不同只有我把信号的定义放在了__init__函数里和函数外,但是第二段程序会报错
AttributeError: 'PyQt5.QtCore.pyqtSignal' object has no attribute 'connect'
请问这个问题是肿么回事
PS:最近变懒了,这个问题应该看文档就能知道,但是姑且放在这里问一下版里研究Qt的几位版主,先行谢过~
  1. # -*- coding: utf-8 -*-
  2. """发射信号示例"""
  3. import sys
  4. from PyQt5 import QtWidgets, QtCore


  5. class EmitSignal(QtWidgets.QWidget):
  6.     closeEmitApp = QtCore.pyqtSignal()  # 这里---------------------------------------------

  7.     def __init__(self):
  8.         super(EmitSignal, self).__init__()

  9.         self.setWindowTitle("发射信号演示程序")
  10.         self.resize(250, 150)

  11.         self.closeEmitApp.connect(self.close)

  12.     def mousePressEvent(self, QMouseEvent):
  13.         self.closeEmitApp.emit()

  14. app = QtWidgets.QApplication(sys.argv)
  15. es = EmitSignal()
  16. es.show()
  17. sys.exit(app.exec_())
复制代码
  1. # -*- coding: utf-8 -*-
  2. """发射信号示例"""
  3. import sys
  4. from PyQt5 import QtWidgets, QtCore


  5. class EmitSignal(QtWidgets.QWidget):

  6.     def __init__(self):
  7.         super(EmitSignal, self).__init__()

  8.         self.setWindowTitle("发射信号演示程序")
  9.         self.resize(250, 150)

  10.         self.closeEmitApp = QtCore.pyqtSignal()  # 这里-------------------------------------
  11.         self.closeEmitApp.connect(self.close)

  12.     def mousePressEvent(self, QMouseEvent):
  13.         self.closeEmitApp.emit()

  14. app = QtWidgets.QApplication(sys.argv)
  15. es = EmitSignal()
  16. es.show()
  17. sys.exit(app.exec_())
复制代码


想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2015-4-17 15:38:59 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-4-17 23:32:07 | 显示全部楼层
最近我也比较懒!这位应该有空->@wei_Y
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-4-20 20:20:47 | 显示全部楼层
这个应该和python类有关系。
类那块没好好学,专业解释也不太清楚。
写一下我发现的不同吧。
  1. class A:
  2.     a = 1

  3.     def __init__(self):
  4.         self.b = 2

  5. print(dir(A))
复制代码

这里我发现类A中的属性只有a而没有b,所以类里面的__init__生成的变量应该不能算是类的属性,应该只能算是类中函数可以共用的变量,类似闭包。(在其他方法中定义变量到另一个函数中无法使用。)

然后,在楼主的代码里查看了一下这两个信号的类型。
发现第一个是<class 'PyQt5.QtCore.pyqtBoundSignal'>
第二个是<class 'PyQt5.QtCore.pyqtSignal'>
顺便help了一下,第一个是
  1. class pyqtBoundSignal(builtins.object)
  2. |  Methods defined here:
  3. |  
  4. |  __call__(self, /, *args, **kwargs)
  5. |      Call self as a function.
  6. |  
  7. |  __getitem__(self, key, /)
  8. |      Return self[key].
  9. |  
  10. |  __new__(*args, **kwargs) from builtins.type
  11. |      Create and return a new object.  See help(type) for accurate signature.
  12. |  
  13. |  __repr__(self, /)
  14. |      Return repr(self).
  15. |  
  16. |  connect(...)
  17. |      connect(slot, type=Qt.AutoConnection, no_receiver_check=False)
  18. |      
  19. |      slot is either a Python callable or another signal.
  20. |      type is a Qt.ConnectionType.
  21. |      no_receiver_check is True to disable the check that the receiver's C++
  22. |      instance still exists when the signal is emitted.
  23. |  
  24. |  disconnect(...)
  25. |      disconnect([slot])
  26. |      
  27. |      slot is an optional Python callable or another signal.  If it is omitted
  28. |      then the signal is disconnected from everything it is connected to.
  29. |  
  30. |  emit(...)
  31. |      emit(*args)
  32. |      
  33. |      *args are the values that will be passed as arguments to all connected
  34. |      slots.
  35. |  
  36. |  ----------------------------------------------------------------------
  37. |  Data descriptors defined here:
  38. |  
  39. |  signal
  40. |      The signature of the signal that would be returned by SIGNAL()
复制代码

显然发现了connect方法。
但是第二个
  1. <unbound signal >
复制代码

是一个未绑定的信号。
翻了一下pyqt5的文档。发现了这么一段。
  1. Unbound and Bound Signals

  2. A signal (specifically an unbound signal) is a class attribute. When a signal is referenced as an attribute of an instance of the class then PyQt5 automatically binds the instance to the signal in order to create a bound signal. This is the same mechanism that Python itself uses to create bound methods from class functions.
复制代码

让开!我来翻译!(渣翻译,见谅。)

一个信号(特别是一个未绑定的信号)是类中的一个属性当信号作为属性被实例化后,PyQt5会自动绑定到实例信号并创建一个绑定的信号。这和Python自身创建绑定方法的功能类似。

好吧,根据文档,信号得是类的属性才可以。但放在__init__里并不是一个属性,是个啥,不知道,暂且叫他局部变量吧。
@~风介~ 介哥好懒!楼主换了一个萌萌哒的头像!

评分

参与人数 2荣誉 +11 鱼币 +11 贡献 +5 收起 理由
lightninng + 1 + 1 感谢楼主无私奉献!
~风介~ + 10 + 10 + 5 热爱鱼C^_^

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2015-4-20 20:38:25 | 显示全部楼层
wei_Y 发表于 2015-4-20 20:20
这个应该和python类有关系。
类那块没好好学,专业解释也不太清楚。
写一下我发现的不同吧。

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-4-20 22:37:59 | 显示全部楼层
wei_Y 发表于 2015-4-20 20:20
这个应该和python类有关系。
类那块没好好学,专业解释也不太清楚。
写一下我发现的不同吧。

谢谢楼主的回答,学习到了遇到问题的解决办法 ~~
其实我也类也基本跟没学过一样,以前学过C然后直接python,所以关于面向对象,太薄弱了~~
这一段我会放到pyqt那个贴子里的,造福广大鱼友~~
最后PS:头像是我大Saber酱,4月番里最火的Fate stay night UBW~~~
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-6-7 10:07:52 | 显示全部楼层
4楼的朋友正解。一般定义信号,放在类变量里面就可以了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-27 01:20

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表