Qt :: QueuedConnection by QML

I have a C ++ class emitting signal and a QML slot. I need to execute a slot in the same thread after the 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 cannot execute deleteObject while processing the collision, I need to do this after the world step.

+3
source share
2 answers

I don't know how much about QML, but I can suggest a different approach: Take a look at QObject :: deleteLater ()

The object will be deleted when the control returns to the event loop.

Since this is a slot, you can either directly connect your signal to bullet.deleteLater(), or call deleteLaterin the slot deleteObject.

+2
source

, ConnectType Connections . , , Connections.

Connections {
    target: box2dCppEngine
    onBulletCollided: timerHelper.restart()
}

Timer {
    id: timerHelper
    interval: 1
    onTriggered: box2dCppEngine.deleteObject(bullet)
}

, , deleteLater().

+2

All Articles