PDA

View Full Version : Usage of QTableView as gridview



ada10
9th August 2010, 08:11
QTableView provides a row & column headers .. i.e it has a name for every row & every column. How to remove these names & convert it to a standard grid view ? The attached image show what I mean ...

5046

Lykurg
9th August 2010, 08:16
Get the header via QTableView::horizontalHeader() and hide it. See also QTableView::verticalHeader().

ada10
9th August 2010, 08:20
I would also like to set the row count & column count ...but in QTableView there does not seem to be any API for doing so .. how else should I customize the row & column count ?

ChrisW67
9th August 2010, 08:59
The view displays the data in the underlying mode, so the row/column count comes from there. You can hide columns or rows in the view: QTableView::setColumnHidden(). If you just want a table of x by y rows/columns that you can display values in then you can also use QTableWidget.

ada10
9th August 2010, 09:41
If I had say 20 items ( QStandardItems ) to the tableview, it just puts it into one vertical list ... :( why has the QGridView class been removed ?
I plan to add custom items to the table view using custom model class later on ... but it should work with any type of items ... row/column hiding does not work ...

Lykurg
9th August 2010, 09:47
if you want a grid view have a look at QListView and its view modes and grid size!

ada10
9th August 2010, 10:09
Thanks, the above suggestion worked ...

ChrisW67
9th August 2010, 23:13
If I had say 20 items ( QStandardItems ) to the tableview, it just puts it into one vertical list ... :( why has the QGridView class been removed ?

You add items to a model, not a view (QTableWidget just hides the model from you). If you add all the items in column 0 then that is what will be displayed by any view you attach to that model. Take a look at QStandardItemModel::setItem() for a way to set a cell other than column 0, or QStandardItemModel::insertRow() or insertColumn() (the ones that take a list of items) for ways to add whole rows/columns.


I plan to add custom items to the table view using custom model class later on ... but it should work with any type of items ... row/column hiding does not work ...
Yes it does.


#include <QtGui>

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

QStandardItemModel model(8, 8);
for (int row = 0; row < 8; ++row) {
for (int column = 0; column < 8; ++column) {
QStandardItem *item = new QStandardItem(QString("%0, %1").arg(row).arg(column));
model.setItem(row, column, item);
}
}

QTableView v;
v.setModel(&model);
v.setRowHidden(3, true);
v.setColumnHidden(3, true);
v.show();
return app.exec();
}