[PyQt] PyQt5 and QSignalSpy

Sébastien Renaud neitsa at gmail.com
Wed Dec 30 14:00:49 GMT 2015


Hello!

Toolset:
    - Windows 10 x64
    - CPython 3.4.3 x64
   - PyQt GPL v5.5.1 for Python v3.4 (x64)

I'm trying to get the hang of QSignalSpy, but I'm probably using it wrongly.

I looked at the documentation [1] and the PyQt sip file [2] for QSignalSpy
to see which methods or properties are available for this object, but I
still don't understand what kind of result I should expect.

[1] https://doc.qt.io/qt-5/qsignalspy.html
[2]https://github.com/pyqt/python-qt5/blob/master/PyQt5/sip/PyQt5/QtTest/qsi
gnalspy.sip

Repro Code:

------------------------------------------------------------
#!/usr/local/bin/python3
# -*- coding: utf8 -*-

from PyQt5.QtTest import QSignalSpy
from PyQt5.QtCore import QObject, QUrl, pyqtSlot
from PyQt5.QtWidgets import QApplication
from PyQt5.QtNetwork import QNetworkRequest, QNetworkReply,\
    QNetworkAccessManager


class HttpRequest(QObject):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.network_access_man = QNetworkAccessManager(self)
        self.network_access_man.finished.connect(self.finished)

    def get(self, url):
        req = QNetworkRequest(QUrl(url))
        self.network_access_man.get(req)

    @pyqtSlot(QNetworkReply)
    def finished(self, reply):
        reply_error = reply.error()
        print("reply error: {}".format(reply_error))
        if reply_error == QNetworkReply.NoError:
            reply_content = reply.readAll()
            print("content:\n{}\n".format(reply_content[0:20]))
        else:
            print("Reply error...")


if __name__ == "__main__":
    app = QApplication([])
    http_req = HttpRequest()
    http_req.get("https://www.python.org/static/files/pubkeys.txt")
    spy = QSignalSpy(http_req.network_access_man.finished)
    print("Spy signal: {}".format(spy.signal()))
    print("Spy isValid:", spy.isValid())
    print("Spy wait return value: {}".format(spy.wait(timeout=5000)))
    if len(spy) > 0:
        print("Spy length: {}".format(len(spy)))
        print(spy[0])
    else:
        print("spy is empty")

----------------------------------------------------------------------------
-----------------

Output is as follow:

----------------------------------------------------------------------------
-----------------
G:\data\Python\PyQtTest\src>signal_spy.py
Don't know how to handle 'reply', use qRegisterMetaType to register it.
Spy signal: b'finished(QNetworkReply*)'
Spy isValid: True
reply error: 0
content:
b'-----BEGIN PGP PUBLI'

Spy wait return value: True
Spy length: 1
[None]
----------------------------------------------------------------------------
-----------------

I was expecting to get a QNetworkReply instance in spy[0] but I get a
NoneType (both isValid()  and wait() QSignalSpy methods returns True)

I'm also worried about the output "Don't know how to handle 'reply', use
qRegisterMetaType to register it."

* Am I doing something wrong?
    - If yes, How should QSignalSpy should be used with PyQt? 
* Is the problem located elsewhere?

Thank you!
Happy holidays and best wishes for 2016!

Seb.



More information about the PyQt mailing list