Qt 中 QTableWidget 的堆使用情况

发布于 2025-01-03 02:31:29 字数 680 浏览 1 评论 0原文

我试图强调我的程序,它基本上用不同的行数更新 QTableWidget

每次我进行某些操作时,我想修改表格的大小,但在它之前如果想清除它及其单元格内容。

我所经历的是,从我的资源监视器监控到的堆只会增加。

这是我在应用程序中按下按钮时运行的代码部分:

MyClass::updateTable(int nrows)
{
    ui->tableWidget->clearContents(); // this is to free the memory but the heap always grows
    for (int i=0; i<nrows; i++)
    {
        // I don't like this new I don't know when the destructor is called here!!
        QTableWidgetItem *item = new QTableWidgetItem();
        item->setText("SOMETEXT");
        ui->tableWidget->setItem(i,0,idItem);
    }
}

int nrows 指定的行数是一个非常可变的数字(从 10 到 10^5 )。

如何从堆中完全清除内存?

I'm trying to stress my program which basically updates with a different number of rows a QTableWidget

Every time I make some action I want to modify the size of the table, but before it if would like to clear it and with it its cell contents.

What I'm experiencing is that the heap as monitored from my resource monitor, only increases.

This is the part of code I run when I press a button in my app:

MyClass::updateTable(int nrows)
{
    ui->tableWidget->clearContents(); // this is to free the memory but the heap always grows
    for (int i=0; i<nrows; i++)
    {
        // I don't like this new I don't know when the destructor is called here!!
        QTableWidgetItem *item = new QTableWidgetItem();
        item->setText("SOMETEXT");
        ui->tableWidget->setItem(i,0,idItem);
    }
}

the number of rows as specified from int nrows is a very variable number ( from 10 to 10^5 ).

How do I clean the memory completely from the heap?

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

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

发布评论

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

评论(1

旧城空念 2025-01-10 02:31:29

这个新的东西是必要的。
奇怪的是,clearContents() 应该摆脱它们。
您可以使用的另一个函数是clear()函数,但这两个函数之间的唯一区别是clear将首先删除标题,然后调用clearContents(),这是实际删除项目的函数。

事实上,看看这些函数的代码:

void QTableModel::clear()
{
    for (int j = 0; j < verticalHeaderItems.count(); ++j) {
        if (verticalHeaderItems.at(j)) {
            verticalHeaderItems.at(j)->view = 0;
            delete verticalHeaderItems.at(j);
            verticalHeaderItems[j] = 0;
        }
    }
    for (int k = 0; k < horizontalHeaderItems.count(); ++k) {
        if (horizontalHeaderItems.at(k)) {
            horizontalHeaderItems.at(k)->view = 0;
            delete horizontalHeaderItems.at(k);
            horizontalHeaderItems[k] = 0;
        }
    }
    clearContents();
}

void QTableModel::clearContents()
{
    for (int i = 0; i < tableItems.count(); ++i) {
        if (tableItems.at(i)) {
            tableItems.at(i)->view = 0;
            delete tableItems.at(i); //Your item should get deleted here
            tableItems[i] = 0;
        }
    }
    reset();
}

你确定这是你泄漏的地方吗?

That new is necessary.
The weird thing is that clearContents() should get rid of them.
The other function you could use is the clear() function, but the only difference between those two is that clear will delete the headers first and then call clearContents(), which is the one that does the actual deletion of the items.

In fact, look at the code for those functions:

void QTableModel::clear()
{
    for (int j = 0; j < verticalHeaderItems.count(); ++j) {
        if (verticalHeaderItems.at(j)) {
            verticalHeaderItems.at(j)->view = 0;
            delete verticalHeaderItems.at(j);
            verticalHeaderItems[j] = 0;
        }
    }
    for (int k = 0; k < horizontalHeaderItems.count(); ++k) {
        if (horizontalHeaderItems.at(k)) {
            horizontalHeaderItems.at(k)->view = 0;
            delete horizontalHeaderItems.at(k);
            horizontalHeaderItems[k] = 0;
        }
    }
    clearContents();
}

void QTableModel::clearContents()
{
    for (int i = 0; i < tableItems.count(); ++i) {
        if (tableItems.at(i)) {
            tableItems.at(i)->view = 0;
            delete tableItems.at(i); //Your item should get deleted here
            tableItems[i] = 0;
        }
    }
    reset();
}

Are you sure this is where you're leaking?

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