Unsolved QTableView rowCount(), setRowCount(), setItem()
-
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!
-
hi
the forum post pointed to these
http://doc.qt.io/qt-5/qabstractitemview.html#model
http://doc.qt.io/qt-5/qabstractitemmodel.html#rowCountso basically, ask for the model, use the models rowcount
-
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' -
@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#setItemso u need to check doc and cast if not in
QAbstractItemModelthe 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. -
Hi,
To add to @mrjj, QTableWidgetItem is a class to be used with QTableWidget not QTableView.
-
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.
-
How many rows are we talking about here ?
How fast do you want to update them ?
-
Right now, 12 rows but might grow to 100 max. Which is faster and how much faster?
-
For that kind of volume QTableWidget is enough.
For speed answers, you'll have to benchmark for your target.
-
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