鱼C论坛

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

关于pyqt5中信号定义的问题

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

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

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

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


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

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

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

        self.closeEmitApp.connect(self.close)

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

app = QtWidgets.QApplication(sys.argv)
es = EmitSignal()
es.show()
sys.exit(app.exec_())
# -*- coding: utf-8 -*-
"""发射信号示例"""
import sys
from PyQt5 import QtWidgets, QtCore


class EmitSignal(QtWidgets.QWidget):

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

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

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

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

app = QtWidgets.QApplication(sys.argv)
es = EmitSignal()
es.show()
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类有关系。
类那块没好好学,专业解释也不太清楚。
写一下我发现的不同吧。
class A:
    a = 1

    def __init__(self):
        self.b = 2

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

然后,在楼主的代码里查看了一下这两个信号的类型。
发现第一个是<class 'PyQt5.QtCore.pyqtBoundSignal'>
第二个是<class 'PyQt5.QtCore.pyqtSignal'>
顺便help了一下,第一个是
class pyqtBoundSignal(builtins.object)
 |  Methods defined here:
 |  
 |  __call__(self, /, *args, **kwargs)
 |      Call self as a function.
 |  
 |  __getitem__(self, key, /)
 |      Return self[key].
 |  
 |  __new__(*args, **kwargs) from builtins.type
 |      Create and return a new object.  See help(type) for accurate signature.
 |  
 |  __repr__(self, /)
 |      Return repr(self).
 |  
 |  connect(...)
 |      connect(slot, type=Qt.AutoConnection, no_receiver_check=False)
 |      
 |      slot is either a Python callable or another signal.
 |      type is a Qt.ConnectionType.
 |      no_receiver_check is True to disable the check that the receiver's C++
 |      instance still exists when the signal is emitted.
 |  
 |  disconnect(...)
 |      disconnect([slot])
 |      
 |      slot is an optional Python callable or another signal.  If it is omitted
 |      then the signal is disconnected from everything it is connected to.
 |  
 |  emit(...)
 |      emit(*args)
 |      
 |      *args are the values that will be passed as arguments to all connected
 |      slots.
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |  
 |  signal
 |      The signature of the signal that would be returned by SIGNAL()
显然发现了connect方法。
但是第二个
<unbound signal >
是一个未绑定的信号。
翻了一下pyqt5的文档。发现了这么一段。
Unbound and Bound Signals

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.
让开!我来翻译!(渣翻译,见谅。)

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

好吧,根据文档,信号得是类的属性才可以。但放在__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-9-20 08:56

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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