Results 1 to 10 of 10

Thread: Count the number of rows in a QTableView

  1. #1
    Join Date
    Jun 2009
    Location
    México
    Posts
    20
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Count the number of rows in a QTableView

    Hi!!! I want to count the number of rows in a QTableView, but i can't figure out how, i read that you use this protected member:

    int QTableView::numRows () const [protected]
    Returns the number of rows in the table.

    and also you have to include this file <qtableview.h>, but no good results so far. THANKS FOR YOUR HELP!!!

    Also i want to change the colors of the rows, any ideas???

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Count the number of rows in a QTableView

    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. The following 3 users say thank you to spirit for this useful post:

    Cupidvogel (12th June 2015), Dong Back Kim (4th August 2011), grub87 (17th June 2009)

  4. #3
    Join Date
    Jun 2009
    Location
    México
    Posts
    20
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Count the number of rows in a QTableView

    Hi again, i'm using already a model in my QTableView but i don't understand hoy to implement "QAbstractItemModel::rowCount" thanks!!!!

  5. #4
    Join Date
    Apr 2006
    Location
    Denmark / Norway
    Posts
    67
    Thanks
    3
    Thanked 12 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Count the number of rows in a QTableView

    Did you create your own model from QAbstractItemModel, or did you create a QStandardItemModel?

    if you used QStandardItemModel for your table, it has a implementation of rowCount you can use. if you created a QAbstractItemModel, you need to subclass it and supply your own rowcount function.

    If you only have a TableView, without a model, read about the MVC system in Qt here and here and here, or use QTableWidget instead.

  6. The following user says thank you to luf for this useful post:

    grub87 (17th June 2009)

  7. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Count the number of rows in a QTableView

    Do you want to know the number of rows visible in the display of the table, or the number of rows in the data model that underlies the view?

    The numRows() method looks like it's a Qt3 or Qt3-compatibility layer method in Assistant. Given you are using Qt4 and QTableView this is not useful to you.

    You already have an answer for the model row count. You might also need to look at canFetchMore() and fetchMore() to make sure you have a true record count.

    I'm not sure that there is a way to get the former.

  8. The following 2 users say thank you to ChrisW67 for this useful post:

    Dong Back Kim (4th August 2011), grub87 (17th June 2009)

  9. #6
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Count the number of rows in a QTableView

    Hi again, i'm using already a model in my QTableView but i don't understand hoy to implement "QAbstractItemModel::rowCount" thanks!!!!
    If you are using your own model, YOU are supposed to provide a count for rowCount. You must be holding some information in structure/database. The rowCount will be equal to the number of such objects.

    If you are not so comfortable with model/view, you can use QTableWidget as luf had suggested. It wraps the things for you.

  10. The following user says thank you to aamer4yu for this useful post:

    grub87 (17th June 2009)

  11. #7
    Join Date
    Jun 2009
    Location
    México
    Posts
    20
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Count the number of rows in a QTableView

    Thank you all for the help, i could count the rows by doing the following:

    Qt Code:
    1. int cont = 0;
    2. if (query.isActive())
    3. while(query.next()){
    4. cont++;
    5. }
    To copy to clipboard, switch view to plain text mode 

    Since i'm using a QSqlQueryModel, i thought about numRowsAffected() or size() but it didn't work out. Thanks!!!

  12. #8
    Join Date
    Apr 2006
    Location
    Denmark / Norway
    Posts
    67
    Thanks
    3
    Thanked 12 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Count the number of rows in a QTableView

    This is depending on the backend of the sqldriver:
    you can call see : QSqlDriver::hasFeature

    If the driver has QSqlDriver::QuerySize, rowcount will work on the model. If not, you can do what you do above.

    Most sql databases have some way of reporting rows with a count query.
    Ordinary select: "SELECT * FROM mytable WHERE data='123'"
    Counting select: "SELECT COUNT(*) FROM mytable WHERE data='123'" (gives you the number of rows that have data = 123, while the first gives you all the rows that have data 123...)

    Examples for mysql here:
    http://dev.mysql.com/doc/refman/5.1/...ting-rows.html

    Will be similar for other SQL databases, but see the docs for the specific database you are using.

  13. #9
    Join Date
    Jun 2009
    Location
    Bato, Leyte, Philippines
    Posts
    10
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Count the number of rows in a QTableView

    I'm posting these codes to try to help you solve your problem. It works on my simple database project.

    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName(":genes:");

    QSqlTableModel *empmodel = new QSqlTableModel();
    empmodel->setTable("person");
    empmodel->select(); // populate records

    QString s;
    s= s.setNum(empmodel->rowCount()); // returns the number of rows

    this->setWindowTitle(s); // display it

    empmodel->setHeaderData(0, Qt::Horizontal, tr("ID"));
    empmodel->setHeaderData(1, Qt::Horizontal, tr("NAME"));
    empmodel->setHeaderData(2, Qt::Horizontal, tr("ADDRESS"));

    QTableView *empview = new QTableView();
    empview->setModel(empmodel);
    empview->resize(QSize::QSize(800,250));
    empview->resizeColumnsToContents();

    empview->show();

  14. #10
    Join Date
    Sep 2008
    Location
    Poland
    Posts
    80
    Thanks
    4
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Count the number of rows in a QTableView

    The best way to count rows:
    tableView.verticalHeader()->count();
    I don't remember if that count() returns the total or only visibles row number-check it by yourself.

  15. The following 2 users say thank you to MasterBLB for this useful post:

    Ashkan_s (11th October 2012), lxman (31st July 2011)

Similar Threads

  1. Set height of QTableView to fit exact number of rows.
    By Ben.Hines in forum Qt Programming
    Replies: 3
    Last Post: 17th January 2019, 01:49
  2. QTableView has constant number of rows
    By tomeks in forum Qt Programming
    Replies: 5
    Last Post: 10th December 2008, 15:29
  3. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  4. Replies: 6
    Last Post: 5th March 2006, 21:05
  5. QTableView number of rows and scrollbar issue
    By jnk5y in forum Qt Programming
    Replies: 3
    Last Post: 1st March 2006, 06:55

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.