PDA

View Full Version : Count the number of rows in a QTableView



grub87
16th June 2009, 18:30
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???

spirit
16th June 2009, 18:57
use QTableView::model and then QAbstractItemModel::rowCount.

grub87
16th June 2009, 19:50
Hi again, i'm using already a model in my QTableView but i don't understand hoy to implement "QAbstractItemModel::rowCount" thanks!!!!

luf
16th June 2009, 22:51
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 (http://doc.trolltech.com/qq/qq10-mvc.html) and here (http://blog.wysota.eu.org/index.php/2005/11/18/mvc-environment-in-qt-40/) and here (http://doc.trolltech.com/latest/model-view-programming.html), or use QTableWidget instead.

ChrisW67
17th June 2009, 00:34
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.

aamer4yu
17th June 2009, 05:05
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.

grub87
17th June 2009, 15:49
Thank you all for the help, i could count the rows by doing the following:



int cont = 0;
if (query.isActive())
while(query.next()){
cont++;
}


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

luf
17th June 2009, 20:52
This is depending on the backend of the sqldriver:
you can call see : QSqlDriver::hasFeature (http://doc.trolltech.com/4.5/qsqldriver.html#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/en/counting-rows.html

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

genessr
27th June 2009, 13:45
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();

MasterBLB
28th June 2009, 16:31
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.