Qt 使用QListView的内存泄漏

Qt 使用QListView的内存泄漏,qt,memory-leaks,qlistview,Qt,Memory Leaks,Qlistview,我正在开发一个应用程序启动器,并决定使用QListView来显示项目。我写了一个模型和一个ItemDelegate,但它似乎在我找不到的地方浪费内存。 问题是,即使在应用程序列表上滚动也会使我的程序消耗越来越多的内存。 我认为这与我的绘画方式有关,但我看不出哪里有错 QStyledItemDelegate上的绘制方法 void DashViewItemDelegate::paint(QPainter * painter, const QStyleOptionViewItem & opti

我正在开发一个应用程序启动器,并决定使用QListView来显示项目。我写了一个模型和一个ItemDelegate,但它似乎在我找不到的地方浪费内存。 问题是,即使在应用程序列表上滚动也会使我的程序消耗越来越多的内存。 我认为这与我的绘画方式有关,但我看不出哪里有错

QStyledItemDelegate上的绘制方法

void DashViewItemDelegate::paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const {
    QString name = index.model()->data(index, Qt::DisplayRole).toString();
    QIcon icon = index.model()->data(index, Qt::DecorationRole).value<QIcon>();
    QRect iconRect = QRect(option.rect.x() + 10, option.rect.y(), option.rect.width() - 20, option.rect.height() - 43);
    QRect textRect = QRect(option.rect.x(), option.rect.y() + 60, option.rect.width(), option.rect.height() - 60);

    if (option.state & QStyle::State_Selected)
        painter->fillRect(option.rect, option.palette.highlight());

    icon.paint(painter, iconRect, Qt::AlignCenter | Qt::AlignTop);

    QTextLayout textLayout(name);
    textLayout.setFont(option.font);
    int widthUsed = 0;
    int lineCount = 0;
    textLayout.beginLayout();

    while (++lineCount < 3) { 
        QTextLine line = textLayout.createLine();
        if (!line.isValid())
            break;

    line.setLineWidth(option.rect.width());
    widthUsed += line.naturalTextWidth();
}
    textLayout.endLayout();

    widthUsed += option.rect.width();

    QString newText = painter->fontMetrics().elidedText(name, Qt::ElideRight, widthUsed);
    painter->drawText(textRect, Qt::AlignCenter | Qt::AlignTop | Qt::TextWordWrap , newText);

    painter->save();
}
void DashViewItemDelegate::paint(qPaint*painter,常数QStyleOptionViewItem&option,常数QModelIndex&index)常数{
QString name=index.model()->data(index,Qt::DisplayRole).toString();
QIcon icon=index.model()->data(index,Qt::DecorationRole).value();
QRect iconRect=QRect(option.rect.x()+10,option.rect.y(),option.rect.width()-20,option.rect.height()-43);
QRect textRect=QRect(option.rect.x(),option.rect.y()+60,option.rect.width(),option.rect.height()-60);
if(option.state&QStyle::state_选中)
painter->fillRect(option.rect,option.palete.highlight());
icon.paint(画师、iconRect、Qt::AlignCenter | Qt::AlignTop);
QTextLayout文本布局(名称);
textLayout.setFont(option.font);
int-widthUsed=0;
int lineCount=0;
textLayout.beginLayout();
而(++行数<3){
QTextLine=textLayout.createLine();
如果(!line.isValid())
打破
line.setLineWidth(option.rect.width());
widthUsed+=line.naturalTextWidth();
}
endLayout();
widthUsed+=option.rect.width();
QString newText=painter->fontMetrics().elidedText(名称,Qt::ElideLight,使用的宽度);
画师->绘图文本(textRect,Qt::AlignCenter | Qt::AlignTop | Qt::TextWordWrap,newText);
画师->保存();
}

非常感谢您的帮助

您能展示一下此功能的使用情况吗?您是在某个时候调用painter->end()还是painter->restore()?我只将此itemdelegate设置为视图,它可以根据需要进行绘制。view->setItemDelegate(新建DashViewItemDelegate());如果删除
painter->save()?仍然有效,但当我滚动或更改viewwell的模型时,内存使用量仍在增长。现在,我删除了我的QStyledItemDelegate,并意识到这种奇怪的事情发生在标准Qt的QStyledItemDelegate上。这个问题是否与批量大小有关?