gpt4 book ai didi

c++ - QBoxLayout 的所有者是否成为所有布局小部件的所有者?

转载 作者:IT老高 更新时间:2023-10-28 22:36:30 24 4
gpt4 key购买 nike

我正在查看示例 here ,并且想知道是否没有内存泄漏。我红了谈论主题的文章mem leak on delete .然而,虽然 QWidgets 确实保留了添加到的小部件的所有权,但布局却没有。

从 QT 代码看来,具有布局的父级获得了该布局的所有小部件的所有权。但是我在文档中看不到任何关于此的引用。

Window::Window()
{
editor = new QTextEdit();
QPushButton *sendButton = new QPushButton(tr("&Send message"));

connect(sendButton, SIGNAL(clicked()), this, SLOT(sendMessage()));

QHBoxLayout *buttonLayout = new QHBoxLayout();
buttonLayout->addStretch();
buttonLayout->addWidget(sendButton);
buttonLayout->addStretch();

QVBoxLayout *layout = new QVBoxLayout(this);
layout->addWidget(editor);
layout->addLayout(buttonLayout);

setWindowTitle(tr("Custom Type Sending"));
}

最佳答案

来自 Layout Management :

Tips for Using Layouts

When you use a layout, you do not need to pass a parent when constructing the child widgets. The layout will automatically reparent the widgets (using QWidget::setParent()) so that they are children of the widget on which the layout is installed.

Note: Widgets in a layout are children of the widget on which the layout is installed, not of the layout itself. Widgets can only have other widgets as parent, not layouts.

You can nest layouts using addLayout() on a layout; the inner layout then becomes a child of the layout it is inserted into.

关于c++ - QBoxLayout 的所有者是否成为所有布局小部件的所有者?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10195762/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com