C ++ - 为什么我在堆上创建这些小部件? [英] C++ - Why do I create these widgets on the heap?

查看:99
本文介绍了C ++ - 为什么我在堆上创建这些小部件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用C ++和Qt创建GUI时,您可以创建一个标签,例如:

When creating a GUI with C++ and Qt you can create a label for example like this :

QLabel* label = new QLabel("Hey you!", centralWidgetParent);

这会在堆上创建对象,并且会保留在那里,直到我手动删除它或父对象销毁。我的问题现在是为什么我需要一个指针呢?为什么不在堆栈上创建它?

This creates the object on the heap and will stay there until I either delete it manually or the parent gets destroyed. My question now is why do I need a pointer for that? Why not create it on the stack?

//Create a member variable of Class MainWindow
QLabel label;

//Set parent to show it and give a text so the user can see it
    QWidget* centralWidget = new QWidget(this); //Needed to add widgets to the window
    this->setCentralWidget( centralWidget ); 
    label.setParent(centralWidget);
    label.setText( "Haha" );

这个工作正常,我可以看到标签,它没有消失。

This works fine, I can see the label and it did not vanish.

我们使用C ++中的指针来让事情变得更长,所以我们可以在不同的作用域中使用一个对象。但是当我创建一个成员变量时,它会不会一直保持到对象被销毁?

We use pointers in C++ to let something live longer so we can use an object in various scopes. But when I create a member variable, won't it stay until the object gets destroyed?

编辑:
也许我没有澄清。这是MainWindow类:

Maybe I didn't clarify it enough. This is the MainWindow class:

class MainWindow : public QMainWindow
{
    Q_OBJECT
    QLabel label; //First introduced here...

public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();
};

//Constructor
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    QWidget* centralWidget = new QWidget(this);
    this->setCentralWidget( centralWidget );
    label.setParent(centralWidget);
    label.setText( "Haha" );
}


推荐答案

c> label 超出范围,将调用析构函数( QLabel ::〜QLabel )。从文档

If your label gets out of the scope, the destructor (QLabel::~QLabel) will be called. From the docs:


销毁对象,删除其所有子对象。

Destroys the object, deleting all its child objects.

在堆上 - 你可以把对象放在堆栈上,但是你需要对对象的生命周期负责(在堆上分配数据最有问题的一个问题是谁和什么时候应该删除这些对象?,而在Qt它由层次结构处理 - 每当你删除你的小部件,所有的子小部件将被删除)。

It is not necessary to create object on the heap - you could put the object on the stack, but then you need to be responsible about lifetime of the object (the one of the most problematic issues about allocating data on the heap is the question of "who and when should delete these objects?", and in Qt it is handled by the hierarchy - whenever you delete your widget, all the child widgets will be deleted).

为什么你的程序工作 - 不知道 - 它可能只是不工作(标签在范围结束时被销毁)。另一个问题是 - 如果你没有引用它,你将如何改变标签的文本(例如从一些插槽)?

Why your program works - I don't know - it may just not work (label is destroyed at the end of the scope). Another issue is - how will you change the text of the label (from some slot, for example) if you don't have a reference to it?

编辑我刚刚看到您的标签 Mainwindow 。有一个完整的对象,而不是作为类的成员的对象的指针是完全正确的,因为它不会在 MainWindow 之前被销毁。请注意,如果您像这样创建 MainWindow 的实例:

Edit I just saw that your label is a member of the MainWindow. It is perfectly fine to have a complete objects, and not the pointer to the objects as the member of your class, as it will not be destroyed before MainWindow is. Please note that if you create instance of your MainWindow like this:

MainWindow *w = new MainWindow();

标签

这篇关于C ++ - 为什么我在堆上创建这些小部件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆