6
考慮這個測試用例:爲什麼QObject銷燬稱爲AFTER銷燬的信號?
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對象被破壞,並能不被阻止。「這是爲什麼發生?