相关疑难解决方法(0)

Qt如何删除对象?什么是存储QObjects的最佳方式?

我听说Qt中的对象会自动删除他们的孩子,我想知道在这些情况下会发生什么.

#include <QApplication>
#include <QLabel>
#include <QHBoxLayout>
#include <QWidget>

int main(int argc, char **argv)
{
    QApplication app(argc, argv);
/*
    QLabel label("label");      //  Program will crash. Destruct order is 1. widget, 2. layout, 3. label
    QHBoxLayout layout;         //  But layout will be deleted twice
    QWidget widget;
*/
    QWidget widget;             //  Program doesn't seem to crash but is it safe ? Does Qt use
    QHBoxLayout layout;         //  delete to operate on already destructed children ?
    QLabel label("label");

    layout.addWidget(&label);   //  layout is label's …
Run Code Online (Sandbox Code Playgroud)

c++ qt destruction

14
推荐指数
2
解决办法
3万
查看次数

标签 统计

c++ ×1

destruction ×1

qt ×1