PyQt4.QtCore.pyqtSignal 对象没有属性“connect”;

发布于 2024-09-04 07:22:41 字数 454 浏览 4 评论 0原文

我在我制作的课程中遇到了自定义信号问题。

相关代码:

self.parse_triggered = QtCore.pyqtSignal()

def parseFile(self):
    self.emit(self.parse_triggered)

这两个都属于类:RefreshWidget。 在其父类中,我有:

self.refreshWidget.parse_triggered.connect(self.tabWidget.giveTabsData())

当我尝试运行该程序时,出现错误:

AttributeError: 'PyQt4.QtCore.pyqtSignal' object has no attribute 'connect'

帮助? 提前致谢。

I'm having issues with a custom signal in a class I made.

Relevant code:

self.parse_triggered = QtCore.pyqtSignal()

def parseFile(self):
    self.emit(self.parse_triggered)

Both of those belong to the class: RefreshWidget.
In its parent class I have:

self.refreshWidget.parse_triggered.connect(self.tabWidget.giveTabsData())

When I try to run the program, I get the error:

AttributeError: 'PyQt4.QtCore.pyqtSignal' object has no attribute 'connect'

Help?
Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

┼── 2024-09-11 07:22:42

如果您无法在自定义类中调用 super()QObject.__init__(),您也会收到该错误消息。

在 Python 中的 Qt 类中定义自定义信号的清单:

  • 您的类(直接或间接)派生​​自 QObject
  • 您的类 __init__ 调用 super() (或调用 QObject.__init__() 直接。)
  • 您的信号被定义为类变量,而不是实例变量
  • 信号的签名(形式参数)与您将连接到信号的任何插槽的签名相匹配,例如<代码>()或(int)(str)((int,),(str,))

You also get that error message if you fail to call super() or QObject.__init__() in your custom class.

A checklist for defining custom signals in a class in Qt in Python:

  • your class derives from QObject (directly or indirectly)
  • your class __init__ calls super() (or calls QObject.__init__() directly.)
  • your signal is defined as a class variable, not an instance variable
  • the signature (formal arguments) of your signal matches the signature of any slot that you will connect to the signal e.g. () or (int) or (str) or ((int,), (str,))
零度℉ 2024-09-11 07:22:42

我最近开始使用 PySide(诺基亚自己的 PyQt 版本),并看到了与自定义新型信号完全相同的行为(和解决方案)。我对该解决方案最大的担忧是,当我有该类的多个实例(在我的例子中为 QThreads)时,使用类变量来保存信号会使事情变得混乱。

据我所见,QtCore.QObject.__init__(self) 在类中找到 Signal 变量,并为实例创建该 Signal 的副本。我不知道 QObject.__init__() 做什么,但生成的 Signal 执行正确的 connect()disconnect()类 Signal 或在 QObject 派生类外部创建的独立 Signal 变量没有这些方法,并且不能正确使用。

I have recently started working with PySide (Nokia's own version of PyQt), and saw the exact same behaviour (and solution) with custom new-style signals. My biggest concern with the solution was that using a class variable to hold the signal would mess things up when I have multiple instances of that class (QThreads in my case).

From what I could see, QtCore.QObject.__init__(self) finds the Signal variable in the class and creates a copy of that Signal for the instance. I have no idea what QObject.__init__() does, but the resulting Signal does proper connect(), disconnect() and emit() methods (and also a __getitem__() method), whereas the class Signal or standalone Signal variables created outside of a QObject-derived class do not have these methods and can't be used properly.

故人的歌 2024-09-11 07:22:42

要使用信号/槽系统,您需要有一个 QObject 继承类。

这是一个简单的例子:



    from PySide import QtCore
    class LivingBeing(QtCore.QObject):
      bornSignal = QtCore.Signal() # initialise our signal

      def __init__(self,name):
        QtCore.QObject.__init__(self) # initialisation required for object inheritance
        self.bornSignal.connect(self.helloWorld) # connect the born signal to the helloworld function
        self.name = name #
        self.alive = False

      def summonFromClay(self):
        self.alive = True
        self.bornSignal.emit() # emit the signal

      def helloWorld(self):
         print "Hello World !, my name is %s, this place is so great !" % self.name

    # now try the little piece of code
    if __name__ == '__main__':
      firstHuman = LivingBeing('Adam')
      firstHuman.summonFromClay()

 

To use the signal/slot system you need to have a QObject inherited class.

Here is a simple example:



    from PySide import QtCore
    class LivingBeing(QtCore.QObject):
      bornSignal = QtCore.Signal() # initialise our signal

      def __init__(self,name):
        QtCore.QObject.__init__(self) # initialisation required for object inheritance
        self.bornSignal.connect(self.helloWorld) # connect the born signal to the helloworld function
        self.name = name #
        self.alive = False

      def summonFromClay(self):
        self.alive = True
        self.bornSignal.emit() # emit the signal

      def helloWorld(self):
         print "Hello World !, my name is %s, this place is so great !" % self.name

    # now try the little piece of code
    if __name__ == '__main__':
      firstHuman = LivingBeing('Adam')
      firstHuman.summonFromClay()

 
感悟人生的甜 2024-09-11 07:22:42

我也有同样的问题。
我忘记了,如果一个类使用信号,那么它必须继承自 QObject。我当时正在做一些重构,并没有注意到这一点。

I had the same problem.
I forgot that if a class uses Signals, then it must inherit from QObject. I was doing some re-factoring and did not pay attention to this.

怼怹恏 2024-09-11 07:22:42

为什么你直接连接信号,而你可以这样做
self.connect(widget, SIGNAL('parse_triggered()'),listener.listening_method)

其中 self 是例如表单本身,并且可能与侦听器相同

Why do you connect directly to the signal, while you can do
self.connect(widget, SIGNAL('parse_triggered()'), listener.listening_method)?

where self is, for example, the form itself, and may be the same as listener

冷情妓 2024-09-11 07:22:41

我和你有同样的问题。

尝试

self.parse_triggered = QtCore.pyqtSignal()

从构造函数中移出,但在类声明中。因此,它不应该看起来像这样:

class Worker(QtCore.QThread):
    def __init__(self, parent = None):
        super(Worker, self).__init__(parent)

        self.parse_triggered = QtCore.pyqtSignal()

它应该看起来像这样:

class Worker(QtCore.QThread):
    parse_triggered = QtCore.pyqtSignal()

    def __init__(self, parent = None):
        super(Worker, self).__init__(parent)

这可能根本不是您正在寻找的东西,但它对我有用。无论如何,我切换回旧式信号,因为我还没有找到新式信号中具有未定义数量或类型的参数的方法。

I had the same exact problem as you.

Try moving

self.parse_triggered = QtCore.pyqtSignal()

out of your constructor but inside your class declaration. So instead of it looking like this:

class Worker(QtCore.QThread):
    def __init__(self, parent = None):
        super(Worker, self).__init__(parent)

        self.parse_triggered = QtCore.pyqtSignal()

It should look like this:

class Worker(QtCore.QThread):
    parse_triggered = QtCore.pyqtSignal()

    def __init__(self, parent = None):
        super(Worker, self).__init__(parent)

This might not be at all what you are looking for, but it worked for me. I switched back to old-style signals anyways because I haven't found a way in new-style signals to have an undefined number or type of parameters.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文