在Qt中,所有的 QPaintDevice 必须要在有 QApplication 实例的情况下创建和使用。如果我们的程序,在 app 退出时,组件却没有关闭,这就会造成程序崩溃。
如下例:
#include <QApplication>
#include <QLabel>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QLabel *label = new QLabel("Hello, world");
label->show();
return app.exec();
}
label 是建立在堆上的,app 是建立在栈上的。这意味着,label 会在 app 之后析构。也就是说,label 的生命周期长于 app 的生命周期。