Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. In a ListView (c++) , how could I retrieve items count?
Forum Updated to NodeBB v4.3 + New Features

In a ListView (c++) , how could I retrieve items count?

Scheduled Pinned Locked Moved General and Desktop
10 Posts 5 Posters 10.5k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MSDQuick
    wrote on last edited by MSDQuick
    #1

    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.

    Joel BodenmannJ V 2 Replies Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Since you are using a QListView, this means that you have a custom model. Query that information from the model with rowCount.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • M MSDQuick

        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.

        Joel BodenmannJ Offline
        Joel BodenmannJ Offline
        Joel Bodenmann
        wrote on last edited by Joel Bodenmann
        #3

        The QListView doesn't have a count() 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 calling QAbstractItemModel::rowCount().

        Edit: Seems like @SGaist was a couple of seconds faster ;)

        Industrial process automation software: https://simulton.com
        Embedded Graphics & GUI library: https://ugfx.io

        1 Reply Last reply
        1
        • M MSDQuick

          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.

          V Offline
          V Offline
          Vladislav Lisovenko
          wrote on last edited by
          #4

          @MSDQuick Guys are right about model, but you can also try this code:

          list.children().count()
          

          Where there's a will, there is a way.

          Joel BodenmannJ 1 Reply Last reply
          -1
          • V Vladislav Lisovenko

            @MSDQuick Guys are right about model, but you can also try this code:

            list.children().count()
            
            Joel BodenmannJ Offline
            Joel BodenmannJ Offline
            Joel Bodenmann
            wrote on last edited by
            #5

            I am not an expert but I am pretty sure that that's wrong. The children() method is actually QWidget::children(). That will return a list of children of that widget - not a list of items that the list widget holds.

            Industrial process automation software: https://simulton.com
            Embedded Graphics & GUI library: https://ugfx.io

            V 1 Reply Last reply
            0
            • Joel BodenmannJ Joel Bodenmann

              I am not an expert but I am pretty sure that that's wrong. The children() method is actually QWidget::children(). That will return a list of children of that widget - not a list of items that the list widget holds.

              V Offline
              V Offline
              Vladislav Lisovenko
              wrote on last edited by
              #6

              @Joel-Bodenmann It seems you are right and I haven't understood documentation, sorry.

              Where there's a will, there is a way.

              1 Reply Last reply
              0
              • VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by
                #7

                You can use ui->myListView->model()->rowCount()

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                1 Reply Last reply
                1
                • M Offline
                  M Offline
                  MSDQuick
                  wrote on last edited by
                  #8

                  Thanks everyone :

                  ui->myListView->model()->rowCount()

                  works fine.

                  Joel BodenmannJ 1 Reply Last reply
                  0
                  • M MSDQuick

                    Thanks everyone :

                    ui->myListView->model()->rowCount()

                    works fine.

                    Joel BodenmannJ Offline
                    Joel BodenmannJ Offline
                    Joel Bodenmann
                    wrote on last edited by
                    #9

                    Just make sure - as always - that you check the pointer returned from QListView::model() before accessing it to avoid nasty crashes.

                    Industrial process automation software: https://simulton.com
                    Embedded Graphics & GUI library: https://ugfx.io

                    M 1 Reply Last reply
                    1
                    • Joel BodenmannJ Joel Bodenmann

                      Just make sure - as always - that you check the pointer returned from QListView::model() before accessing it to avoid nasty crashes.

                      M Offline
                      M Offline
                      MSDQuick
                      wrote on last edited by
                      #10

                      @Joel-Bodenmann
                      Thank you Joel.

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved