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. -
Hi,
Since you are using a QListView, this means that you have a custom model. Query that information from the model with rowCount.
-
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 ;)
-
@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. -
@Joel-Bodenmann It seems you are right and I haven't understood documentation, sorry.
-
You can use
ui->myListView->model()->rowCount()
-
Thanks everyone :
ui->myListView->model()->rowCount()
works fine.
-
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.