QML QObject destroyed before being able to process a signal

天涯浪子 提交于 2019-12-13 05:59:18

问题


In QML, I'm using a C++ library that returns a QObject that does a process and emits a signal when is done. In javascript, I use the connect method of the signal being emitted (success) to attach an anonymous function that should handle the signal as the following piece of code shows:

var requestResponse = apiClient.execute(reqInp);
requestResponse.success.connect(function success(response) 
{
        var requestResponseJSON = JSON.parse(response.responseAsJsonString());
        this.append(response.responseAsJsonString());
});

My problem is that sometimes the QML item that contains this method is destroyed before the C++ code being able to complete, so when the signal is emitted, the anonymous function causes an error, because it calls methods that are undefined (in my example, the method append). I have some nasty crashes in iOS and I suspect that this is what might be causing it.

Is there a way of force disconnection of the signal when the object that created the function is destroyed?


回答1:


var requestResponse = apiClient.execute(reqInp);
function myFunction(response) 
{
        var requestResponseJSON = JSON.parse(response.responseAsJsonString());
        this.append(response.responseAsJsonString());
}
requestResponse.success.connect(myFunction);
requestResponse.destroyed.disconnect(myFunction)


来源:https://stackoverflow.com/questions/30850030/qml-qobject-destroyed-before-being-able-to-process-a-signal

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