Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyQt emit signal with dict

I wanna to emit signal that is defined:

finished = pyqtSignal(dict)

# other place it's connected to function:
def finised(self, dict_result):

I call it self.finished.emit({"bk": {}}) and it works.
Now I call it with self.finished.emit({2: {}})and it don't work!!

Traceback (most recent call last): File "/home/sylwek/workspace/t2-pv/Manager.py", line 452, in run self.finished.emit({2: {}}) TypeError: TesterManager.finished[dict].emit(): argument 1 has unexpected type 'dict'

Is it normal? I can change {2: {}} to {'2': {}} but I would like to understand why and be sure there will be no other surprises!

I use PyQt 5.8.2-2 and python 3.6.1-1

EDIT (add working example):

from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QApplication


class Emiterer(QtCore.QThread):
    f = QtCore.pyqtSignal(dict)

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

    def run(self):
        self.f.emit({"2": {}})
        # self.f.emit({2: {}})  < == this don't work!


class Main(QtWidgets.QMainWindow):

    def __init__(self):
        super(Main, self).__init__()
        self.e = Emiterer()
        self.e.f.connect(self.finised)
        self.e.start()

    def finised(self, r_dict):
        print(r_dict)


if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    m = Main()
    m.show()
    sys.exit(app.exec_())
like image 836
S.R Avatar asked May 14 '17 14:05

S.R


1 Answers

Use object instead of dict in the pyqtSignal definition. E.g.

class Emiterer(QtCore.QThread):
    f = QtCore.pyqtSignal(object)

The reason for this is that signals defined as pyqtSignal(dict) are actually interpreted the same as pyqtSignal('QVariantMap') by PyQt5 and QVariantMap can only have strings as keys.

You can check this (for your specific class) with

m = Emiterer.staticMetaObject
method = m.method(m.methodCount() - 1)  # the last defined signal or slot
print(method.methodSignature())

This would print PyQt5.QtCore.QByteArray(b'f(QVariantMap)')

like image 113
Aleš Erjavec Avatar answered Sep 17 '22 08:09

Aleš Erjavec