考虑以下测试用例:
class MyObject : public QObject
{
Q_OBJECT
public:
MyObject() { qDebug() << "MyObject constructor"; }
virtual ~MyObject() { qDebug() << "MyObject destructor"; }
};
class Tracker : public QObject
{
Q_OBJECT
public:
Tracker() {}
public slots:
void onDestructor() { qDebug() << "About to be destroyed!"; }
};
int main(int argc, char** argv)
{
QCoreApplication app(argc, argv);
Tracker tracker;
MyObject *obj = new MyObject();
QObject::connect(obj, SIGNAL(destroyed()), &tracker, SLOT(onDestructor()));
delete obj;
return app.exec();
}
它打印出这个:
MyObject constructor
MyObject destructor
About to be destroyed!
这种行为与Qt文档相矛盾:"该信号在对象obj被销毁之前立即发出,无法被阻止。" 为什么会发生这种情况?