Is it safe to disconnect a slot before it was called?

我的梦境 提交于 2019-12-11 07:01:49

问题


I have several objects which need to be updated. The updating is done in a separate thread and the results will be transferred to the objects via a pyqtSignal. So the updating method loops over all objects like this:

The object class:

class MyObject(QObject):
    def __init__(self, id):
        QObject.__init__(self)

        self.id = id

    def catch_result(self, id, res):
        if id == self.id:
            # do something with res

And the worker class:

class Worker(QObject):

    result = pyqtSignal('QString', 'QString')

    def __init__(self):
        QObject.__init__(self)

        self.objects = []
        # create some objects here

    def update_one(self, obj):
        # do some computation -> will produce res
        self.result.emit(obj.id, res)

    def update_all(self):
        for obj in objects:
            self.result.connect(obj.catch_result)
            self.update_one(obj)
            self.result.disconnect(obj.catch_result)  # is this safe?

The moment the slot catch_result will be disconnected from the signal result it is already emitted. From what I understood so far, after emitting a signal it will be placed in the receivers queue and accordingly processed. However assume that the signal will be disconnected before the slot was called, will the slot still be executed correctly? Is it save to disconnect slots before they have been called but after the corresponding signal was emitted?

来源:https://stackoverflow.com/questions/28829553/is-it-safe-to-disconnect-a-slot-before-it-was-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!