在 deleteLater() 之前是否调用了所有 Qt 插槽?
Are all Qt slots called before deleteLater()?
当我运行下面的代码时,两个插槽都会被调用:
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();
结果:
"Slot 1 called"
"Slot 2 called"
deleteLater()
是否总是在调用给定信号的所有未决插槽后执行?
此致,
根据deleteLater()
documentation。
Schedules this object for deletion.
The object will be deleted when control returns to the event loop.
因此,deleteLater()
立即被调用,但此时对象并未被删除。如果有事件循环,调用slots后没有事件发生,则删除对象
当我运行下面的代码时,两个插槽都会被调用:
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();
结果:
"Slot 1 called"
"Slot 2 called"
deleteLater()
是否总是在调用给定信号的所有未决插槽后执行?
此致,
根据deleteLater()
documentation。
Schedules this object for deletion.
The object will be deleted when control returns to the event loop.
因此,deleteLater()
立即被调用,但此时对象并未被删除。如果有事件循环,调用slots后没有事件发生,则删除对象