Are all Qt slots called before deleteLater()?

61 Views Asked by At

When I run the following code then both slots are called:

auto packet = new Packet(this);
connect(packet, &Packet::packetFinished, this, [packet]()
{
    qDebug() << "Slot 1 called.";
    packet->deleteLater();
});
connect(packet, &Packet::packetFinished, this, [packet]()
{
    qDebug() << "Slot 2 called.";
});
emit packet->packetFinished();

Results in:

"Slot 1 called"
"Slot 2 called"

Is deleteLater() always executed after all pending slots for a given signal have been called?

Regards,

1

There are 1 best solutions below

0
On BEST ANSWER

According to deleteLater() documentation.

Schedules this object for deletion.
The object will be deleted when control returns to the event loop.

So, deleteLater() is called right away, but the object is not deleted at that point. If there is an event loop, and there is no event after the slots being called, then the object will be deleted.