Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt::QueuedConnection from QML

Tags:

c++

qt

qml

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.

like image 985
psyched Avatar asked Mar 04 '26 10:03

psyched


2 Answers

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.

like image 169
Tim Meyer Avatar answered Mar 06 '26 23:03

Tim Meyer


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().

like image 30
Dynamo72 Avatar answered Mar 07 '26 01:03

Dynamo72