gpt4 book ai didi

c++ - Qt5 QMainWindow组件删除

转载 作者:行者123 更新时间:2023-11-30 02:29:10 25 4
gpt4 key购买 nike

我们的一位老师要求我们创建一个没有任何主窗口 UI 文件的 Qt 应用程序(QMainWindow)。通常我总是创建一个,将其留空并让 uic 处理它。

我知道如果一个widget(child)和它的parent之间定义了parental relation,那么就没有必要删除这个widget(删除parent时删除)。所以,当 UI 被删除时,所有的 child 都被销毁。

如果我们不使用一个UI文件(不是生成的),我们是否必须手动删除所有添加到GUI的widget?

一个小例子:

MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent){
layout = new QHBoxLayout(this);
aButton = new QButton(this);
layout->addWidget(aButton);
...
}

MainWindow::~MainWindow(){
delete ui; // No need to delete more if parental relation.
// However, what do we do if no ui has been generated?
// Do we have to delete aButton?
}

parent值为0,是应用的主入口。

谢谢

最佳答案

请引用this article

QWidget, the fundamental class of the Qt Widgets module, extends the parent-child relationship. A child normally also becomes a child widget, i.e. it is displayed in its parent's coordinate system and is graphically clipped by its parent's boundaries. For example, when the application deletes a message box after it has been closed, the message box's buttons and label are also deleted, just as we'd want, because the buttons and label are children of the message box.

因此,您是否使用 ui 没有区别。当您删除窗口时,它的所有子窗口也将被删除。

关于c++ - Qt5 QMainWindow组件删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39857945/

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