How to connect a destroyed signal of C++ object from QML?(如何从 QML 连接 C++ 对象的破坏信号?)
问题描述
我想从 QML 连接 C++ QObject
的破坏信号,所以我这样做了:
I want to connect a destroyed signal of C++ QObject
from QML so I did this:
Rectangle
{
id: root
width: 128
height: 128
Button
{
anchors.centerIn: parent
text: "Click me"
onClicked:
{
qobj.Component.onDestruction.connect(function(){console.log("It destroy")}) // qobj is set from c++
qobj.destroy() // should output "It destroy"
}
}
但是当我销毁 qobj
时没有打印任何内容.
But nothing is printed when I destroy qobj
.
推荐答案
在一般情况下,您可以使用 连接 元素:
In the general case, you can connect to signals emitted from a C++ object using a Connections element:
Connections {
target: yourObjectComingFromCpp
onSomeSignal: console.log("Something")
}
或者在 Javascript 中通过在 JS 映射对象的相应属性上调用 connect
函数:
or in Javascript by calling the connect
function on the corresponding property of the JS-mapped object:
// without the *on*!
yourObjectComingFromCpp.someSignal.connect( /* JS function here */ );
<小时>
但是:这不适用于特定的 QObject::destroyed
信号,这些信号被强制列入黑名单并且在 QML 中永远不可用(来源).
However: this doesn't work for the specific QObject::destroyed
signals, which are forcibly blacklisted and never available in QML (source).
我猜原因是该对象无论如何都从 QML 上下文中消失了,而且当发出该信号时,您将深入到 QObject 自己的析构函数中,这意味着您的子类上的任何属性或方法访问在那一点.
I guess the reason is that the object is gone from the QML context anyhow at that point, plus when that signal is emitted you're deep into QObject's own destructor, which means any property or method access on your subclass is invalid at that point.
这篇关于如何从 QML 连接 C++ 对象的破坏信号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何从 QML 连接 C++ 对象的破坏信号?


基础教程推荐
- 如何在特定日期之前获取消息? 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01