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. QTableView rowCount(), setRowCount(), setItem()
Forum Updated to NodeBB v4.3 + New Features

QTableView rowCount(), setRowCount(), setItem()

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 4 Posters 24.3k Views 2 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.
  • W Offline
    W Offline
    WhatIf
    wrote on last edited by
    #1

    Hi,

    QTableView doesn't seem to have rowCount(), setRowCount() or setItem(). I was following the code from this post to add rows to QTableView:

    http://www.qtcentre.org/threads/24755-How-to-add-data-into-the-QTableWidget

    I get 4 different errors:

    error: 'class QTableView' has no member named 'rowCount'
    error: 'class QTableView' has no member named 'setRowCount'
    error: 'class QTableView' has no member named 'setItem'
    error: expected type-specifier before 'QTableWidgetItem'

    I searched online to find a solution but the post that has the solution to at the least 3 member functions has broken links:

    https://forum.qt.io/topic/10708/solved-rowcount-tableview/3

    Would greatly appreciate your assistance!

    1 Reply Last reply
    1
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      hi
      the forum post pointed to these
      http://doc.qt.io/qt-5/qabstractitemview.html#model
      http://doc.qt.io/qt-5/qabstractitemmodel.html#rowCount

      so basically, ask for the model, use the models rowcount

      1 Reply Last reply
      2
      • W Offline
        W Offline
        WhatIf
        wrote on last edited by
        #3

        Here is what I did:

        const int currentRow = ui->statisticsTableView->model()->rowCount();
        ui->statisticsTableView->model()->setRowCount(currentRow + 1);
        ui->statisticsTableView->model()->setItem(currentRow, 0, new QTableWidgetItem(ui->statisticsTableView->itemText(i)));
        ui->statisticsTableView->model()->setItem(currentRow, 1, new QTableWidgetItem(getNextStats()));
        

        I'm getting the following 3 error:

        error: 'class QAbstractItemModel' has no member named 'setRowCount'
        error: 'class QAbstractItemModel' has no member named 'setItem'
        error: expected type-specifier before 'QTableWidgetItem'

        1 Reply Last reply
        1
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @WhatIf said:

          Hi, you might need to cast it to the correct model type

          QStandardItemModel has the function
          http://doc.qt.io/qt-5/qstandarditemmodel.html#setItem

          so u need to check doc and cast if not in
          QAbstractItemModel

          the model() return a base class model, NOT the type of the actual model.
          so if you need methods from model subclasses, u might need to cast.

          1 Reply Last reply
          1
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi,

            To add to @mrjj, QTableWidgetItem is a class to be used with QTableWidget not QTableView.

            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
            • W Offline
              W Offline
              WhatIf
              wrote on last edited by
              #6

              One last question, all I'm trying to accomplish is display some statistics, 2 column and many rows. Should I use QTableWidget instead?

              The Docs have snippets code to help get me start. But I'm not really sure if having a default model from QTableWidget helps or hurts in my case.

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                How many rows are we talking about here ?

                How fast do you want to update them ?

                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
                0
                • W Offline
                  W Offline
                  WhatIf
                  wrote on last edited by
                  #8

                  Right now, 12 rows but might grow to 100 max. Which is faster and how much faster?

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    For that kind of volume QTableWidget is enough.

                    For speed answers, you'll have to benchmark for your target.

                    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
                    0
                    • VRoninV Offline
                      VRoninV Offline
                      VRonin
                      wrote on last edited by
                      #10

                      If you really want to understand what's going on see http://doc.qt.io/qt-5/model-view-programming.html and/or chapter 10 of this book http://www.bogotobogo.com/cplusplus/files/c-gui-programming-with-qt-4-2ndedition.pdf

                      "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

                      • Login

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