Qt 容器内存管理

发布于 2025-01-03 09:53:51 字数 1233 浏览 0 评论 0原文

我知道有很多关于Qt内存管理的问题。我还阅读了这些问题:

但就我而言,我又困惑了!

我有一个名为 myTableQTableWidget。我通过 setCellWidget 添加运行时小部件:

void MyClass::build()
{
    for (int i=LOW; i<HIGH; i++)
    {
        QWidget *widget = new QWidget(myTable);
        //
        // ...
        //
        myTable->setCellWidget(i, 0, widget);
    }
}

然后,我删除如下所示的所有项目:

void MyClass::destroy()
{
    for (int i = myTable->rowCount(); i >= 0; --i)
        myTable->removeRow(i);
}

这些方法在很长一段时间内调用多次。 myTable 作为这些小部件的父级将在程序的生命周期中存在。

方法 destroy() 会自动完全释放内存吗?或者我必须自己删除分配的小部件,如下所示?

void MyClass::destroy2() // This maybe causes to crash !!
{
    for (int i = myTable->rowCount(); i >= 0; --i)
    {
        QWidget *w = myTable->cellWidget(i, 0);
        delete w;
        myTable->removeRow(i);
    }
}

I know there is many questions about memory management in Qt. Also I read these SO questions:

But in my case, I confused again!

I have a QTableWidget with name myTable. I add run-time widgets to it by setCellWidget:

void MyClass::build()
{
    for (int i=LOW; i<HIGH; i++)
    {
        QWidget *widget = new QWidget(myTable);
        //
        // ...
        //
        myTable->setCellWidget(i, 0, widget);
    }
}

Then, I delete all items like below:

void MyClass::destroy()
{
    for (int i = myTable->rowCount(); i >= 0; --i)
        myTable->removeRow(i);
}

These methods call many times during long time. And myTable as the parent of those widgets will live along program's life-time.

Dose method destroy() release memory quite and automatically? Or I have to delete allocated widgets myself like below?

void MyClass::destroy2() // This maybe causes to crash !!
{
    for (int i = myTable->rowCount(); i >= 0; --i)
    {
        QWidget *w = myTable->cellWidget(i, 0);
        delete w;
        myTable->removeRow(i);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撩发小公举 2025-01-10 09:53:51

一般来说,当对如何使用类有疑问或困惑时,请查阅该类应附带的文档。幸运的是, QTableWidget::setCellWidget() 确实在事实附带文档

void QTableWidget::setCellWidget ( int row, int columns, QWidget * widget )

设置要在单元格中显示的给定小部件
给定行和列,将小部件的所有权传递给
[强调我的]。如果单元格小部件 A 替换为单元格小部件 B,则单元格小部件
A将被删除。例如,在下面的代码片段中,
QLineEdit 对象将被删除。

 setCellWidget(index, new QLineEdit);
    ...
    setCellWidget(index, 新的 QTextEdit);

调用 myTable->setCellWidget() 后,该表现在拥有您传递给它的小部件。这意味着 myTable 负责删除您传递给 setCellWidget() 的小部件。删除行时,无需执行 delete w; 操作。您的第一个 destroy() 函数应该足够了。

Generally speaking, when in doubt or confused about how to use a class, consult the documentation that should've came with it. Fortunately, QTableWidget::setCellWidget() does in fact come with documentation:

void QTableWidget::setCellWidget ( int row, int column, QWidget * widget )

Sets the given widget to be displayed in the cell in the
given row and column, passing the ownership of the widget to the
table
[emphasis mine]. If cell widget A is replaced with cell widget B, cell widget
A will be deleted. For example, in the code snippet below, the
QLineEdit object will be deleted.

    setCellWidget(index, new QLineEdit);
    ...
    setCellWidget(index, new QTextEdit);

After the call to myTable->setCellWidget(), the table now owns the widget you passed into it. That means that myTable is responsible for deleting the widgets you pass to setCellWidget(). You don't need to do delete w; when you're removing rows. Your first destroy() function should be sufficient.

靑春怀旧 2025-01-10 09:53:51

来自文档

void QTableWidget::setCellWidget ( int row, int columns, QWidget * widget )

将给定的小部件设置为显示在给定行和列的单元格中,并将小部件的所有权传递给表格。

如果单元格小部件 A 被单元格小部件 B 替换,单元格小部件 A 将被删除。例如,在下面的代码片段中,QLineEdit 对象将被删除。

也就是说,您不需要手动清理,因为资源的释放会自动发生在 Qt 对象树中,您的 widget 已成为其中的一部分。

From the documentation:

void QTableWidget::setCellWidget ( int row, int column, QWidget * widget )

Sets the given widget to be displayed in the cell in the given row and column, passing the ownership of the widget to the table.

If cell widget A is replaced with cell widget B, cell widget A will be deleted. For example, in the code snippet below, the QLineEdit object will be deleted.

That is, you don't clean up manually, as freeing of resources happens automatically down the Qt object tree, of which your widget has become a part.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文