求助QlistView

tt2com 2012-09-27 10:30:47
我需要一个经常更新的ListView,一开始我使用QML做的,但由于是动态的,而且数据比较多,造成很严重的内存泄露。
我想回到C++来写,但我看QListView 对于绑定Molde 只有一个简单的SetModel()函数

我想问的是我需要移除Model 该如何实现?

我看到网上 使用SetModel(NULL),这样方法可以清除界面上的显示内容,但我怎么看怎么感觉又内存泄露了。

哪位有这方面的经验的,指导下。
...全文
153 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
嗯哪。 selectionmodel是view的哈。。因为每个view 选中的index是不一样的哈。
不会内存泄漏。。只是内存会增加。。到view释放的时候。之前的这些selectionmodel才会释放。
看代码是这样的。

要不 你setmodel之前。。先用个指针指向这个selectionmodel..

setmodel完了。
再手动 delete 掉旧的selectionmodel.....
tt2com 2012-09-27
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]

QItemSelectionModel *selection_model = new QItemSelectionModel(d->model, this);

这个selection_model不是挂到他的parent上的么。。
当然这个和生命周期是和this一样的哈。。
[/Quote]
可是这个this 是ListView啊 不是demol啊
  • 打赏
  • 举报
回复
QItemSelectionModel *selection_model = new QItemSelectionModel(d->model, this);

这个selection_model不是挂到他的parent上的么。。
当然这个和生命周期是和this一样的哈。。
tt2com 2012-09-27
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

哦。 view里面是保存了个指针哈。 刚看源码不会做什么copy之类的动作。。
ListView取model里数据的时候。只是现取现用哈。

如果你实在不想要delete model就可以了。
[/Quote]
你么看仔细啊。

void QAbstractItemView::setModel(QAbstractItemModel *model)
{
Q_D(QAbstractItemView);
if (model == d->model)
return;
if (d->model && d->model != QAbstractItemModelPrivate::staticEmptyModel()) {
disconnect(d->model, SIGNAL(destroyed()),
this, SLOT(_q_modelDestroyed()));
disconnect(d->model, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
this, SLOT(dataChanged(QModelIndex,QModelIndex)));
disconnect(d->model, SIGNAL(headerDataChanged(Qt::Orientation,int,int)),
this, SLOT(_q_headerDataChanged()));
disconnect(d->model, SIGNAL(rowsInserted(QModelIndex,int,int)),
this, SLOT(rowsInserted(QModelIndex,int,int)));
disconnect(d->model, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)),
this, SLOT(rowsAboutToBeRemoved(QModelIndex,int,int)));
disconnect(d->model, SIGNAL(rowsRemoved(QModelIndex,int,int)),
this, SLOT(_q_rowsRemoved(QModelIndex,int,int)));
disconnect(d->model, SIGNAL(columnsAboutToBeRemoved(QModelIndex,int,int)),
this, SLOT(_q_columnsAboutToBeRemoved(QModelIndex,int,int)));
disconnect(d->model, SIGNAL(columnsRemoved(QModelIndex,int,int)),
this, SLOT(_q_columnsRemoved(QModelIndex,int,int)));
disconnect(d->model, SIGNAL(columnsInserted(QModelIndex,int,int)),
this, SLOT(_q_columnsInserted(QModelIndex,int,int)));

disconnect(d->model, SIGNAL(modelReset()), this, SLOT(reset()));
disconnect(d->model, SIGNAL(layoutChanged()), this, SLOT(_q_layoutChanged()));
}
d->model = (model ? model : QAbstractItemModelPrivate::staticEmptyModel());

// These asserts do basic sanity checking of the model
Q_ASSERT_X(d->model->index(0,0) == d->model->index(0,0),
"QAbstractItemView::setModel",
"A model should return the exact same index "
"(including its internal id/pointer) when asked for it twice in a row.");
Q_ASSERT_X(d->model->index(0,0).parent() == QModelIndex(),
"QAbstractItemView::setModel",
"The parent of a top level index should be invalid");

if (d->model != QAbstractItemModelPrivate::staticEmptyModel()) {
connect(d->model, SIGNAL(destroyed()),
this, SLOT(_q_modelDestroyed()));
connect(d->model, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
this, SLOT(dataChanged(QModelIndex,QModelIndex)));
connect(d->model, SIGNAL(headerDataChanged(Qt::Orientation,int,int)),
this, SLOT(_q_headerDataChanged()));
connect(d->model, SIGNAL(rowsInserted(QModelIndex,int,int)),
this, SLOT(rowsInserted(QModelIndex,int,int)));
connect(d->model, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)),
this, SLOT(rowsAboutToBeRemoved(QModelIndex,int,int)));
connect(d->model, SIGNAL(rowsRemoved(QModelIndex,int,int)),
this, SLOT(_q_rowsRemoved(QModelIndex,int,int)));
connect(d->model, SIGNAL(columnsAboutToBeRemoved(QModelIndex,int,int)),
this, SLOT(_q_columnsAboutToBeRemoved(QModelIndex,int,int)));
connect(d->model, SIGNAL(columnsRemoved(QModelIndex,int,int)),
this, SLOT(_q_columnsRemoved(QModelIndex,int,int)));
connect(d->model, SIGNAL(columnsInserted(QModelIndex,int,int)),
this, SLOT(_q_columnsInserted(QModelIndex,int,int)));

connect(d->model, SIGNAL(modelReset()), this, SLOT(reset()));
connect(d->model, SIGNAL(layoutChanged()), this, SLOT(_q_layoutChanged()));
}

QItemSelectionModel *selection_model = new QItemSelectionModel(d->model, this);

//这个TMD 怎么控制啊,大爷的,每次set 都来一个,我还没看到那边进行处理的。
//但可以肯定的是不断的setModel 我感觉100% 内存泄露
connect(d->model, SIGNAL(destroyed()), selection_model, SLOT(deleteLater()));
setSelectionModel(selection_model);

reset(); // kill editors, set new root and do layout
}
  • 打赏
  • 举报
回复
哦。 view里面是保存了个指针哈。 刚看源码不会做什么copy之类的动作。。
ListView取model里数据的时候。只是现取现用哈。

如果你实在不想要delete model就可以了。
tt2com 2012-09-27
  • 打赏
  • 举报
回复
我在意的是
对于ListView SetModel后 是否其本身有开发新的内存来处理model,那样的话我根本无法处理,如果仅仅是有指针牵引到model的内存块,那样的话处理起来就方便了。

但我看Qt中不少是内部开辟内存的,我就怕这个,那样的话我就郁闷了。我现在在看源码,还没看明白他是否内存开发内存,希望仅仅是牵引
tt2com 2012-09-27
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

e ..你构建model的时候。。一般挂到mianwidget上。。。
其实view->setModel()并不管理model的生命周期。
因为可能有多个view 同时对应与同一个Model。

所以 myModel = new MyModel(this)....不能丢哈。。这样就不泄漏哈。。
[/Quote]
你的意思是我至今delete model 就可以了?
  • 打赏
  • 举报
回复
e ..你构建model的时候。。一般挂到mianwidget上。。。
其实view->setModel()并不管理model的生命周期。
因为可能有多个view 同时对应与同一个Model。

所以 myModel = new MyModel(this)....不能丢哈。。这样就不泄漏哈。。
  • 打赏
  • 举报
回复
是呢。。还好这个能看到源码。知道内存用到哪个地方呢。
tt2com 2012-09-27
  • 打赏
  • 举报
回复
selectionModel();用这个函数将指针导出来手动删。
结贴
tt2com 2012-09-27
  • 打赏
  • 举报
回复
我感觉这个Qt做的很不好,我做的是嵌入式总共没多少内存,而且有些地方不是可以随时delete的,他们挂掉,那么客户就认为机子坏了。Qt在跨平台方面是有不错的能力。但我不知道是不是Qt故意的,有很多内存处理上有有些让人郁闷的地方

16,213

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧