I have C++ class emitting signal and QML slot. I need to execute slot in the same thread after program returns to the event loop.
How can I achieve something like this?
Connections {
target: box2dCppEngine
onBulletCollided: box2dCppEngine.deleteObject(bullet)
connectionType: Qt.QueuedConnection
}
I need this because I can not execute deleteObject, while processing the collision, I need to do this after world step.
I don't know how much about QML but I can offer a different approach: Have a look at QObject::deleteLater()
The object will be deleted when control returns to the event loop.
As it is a slot, you can either connect your signal directly to bullet.deleteLater(), or call deleteLater within your deleteObject slot.
Unfortunately there is no connectionType property in the Connections component. But a simple workaround is to restart a oneshot timer instead of calling the method directly in the Connections signalHandler.
For example
Connections {
target: box2dCppEngine
onBulletCollided: timerHelper.restart()
}
Timer {
id: timerHelper
interval: 1
onTriggered: box2dCppEngine.deleteObject(bullet)
}
But as Tim Meyer pointed out, in your case it might be easier to use deleteLater().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With