问题描述
在QML中,我使用一个C ++库,返回一个执行过程的QObject,并在完成后发出一个信号。在javascript中,我使用发出信号的 connect
方法( success
)附加一个应该处理的匿名函数如下面的代码段所示:
var requestResponse = apiClient.execute(reqInp);
requestResponse.success.connect(function success(response)
{
var requestResponseJSON = JSON.parse(response.responseAsJsonString());
this.append(response.responseAsJsonString ));
});我的问题是,有时包含这个方法的QML项目在C ++代码能够被删除之前就被销毁了。完成,所以当信号被发出时,匿名函数导致错误,因为它调用未定义的方法(在我的例子中,方法 append
)。我在iOS中有一些令人讨厌的崩溃,我怀疑这是可能导致它。
当创建该函数的对象被销毁时,是否有强制断开信号的方法?
解决方案 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)
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?
解决方案 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)
这篇关于QML QObject在能够处理信号之前被销毁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!