In a ListView (c++) , how could I retrieve items count?
-
Hi:
I would like to retrieve number of rows or items in my Listview . I need something like this :qDebug() << QString::number ( ui->myListView->count );
But there is no "count" function in QListView .
Thanks for helping.
MSD.The
QListView
doesn't have acount()
function because it itself doesn't hold any items. That's the job of the model. Hence you have to ask the model for the item count. In case of a list this is simply a matter of callingQAbstractItemModel::rowCount()
.Edit: Seems like @SGaist was a couple of seconds faster ;)
-
Hi:
I would like to retrieve number of rows or items in my Listview . I need something like this :qDebug() << QString::number ( ui->myListView->count );
But there is no "count" function in QListView .
Thanks for helping.
MSD.@MSDQuick Guys are right about model, but you can also try this code:
list.children().count()
-
@MSDQuick Guys are right about model, but you can also try this code:
list.children().count()
I am not an expert but I am pretty sure that that's wrong. The
children()
method is actuallyQWidget::children()
. That will return a list of children of that widget - not a list of items that the list widget holds. -
I am not an expert but I am pretty sure that that's wrong. The
children()
method is actuallyQWidget::children()
. That will return a list of children of that widget - not a list of items that the list widget holds.@Joel-Bodenmann It seems you are right and I haven't understood documentation, sorry.
-
Just make sure - as always - that you check the pointer returned from
QListView::model()
before accessing it to avoid nasty crashes. -
Just make sure - as always - that you check the pointer returned from
QListView::model()
before accessing it to avoid nasty crashes.@Joel-Bodenmann
Thank you Joel.