Update QTableView after hiding columns
-
Why not use a proxy model (based on "QSortFilterProxyModel":http://doc.qt.nokia.com/stable/qsortfilterproxymodel.html) like in this "wiki article":http://developer.qt.nokia.com/wiki/Filter_Columns_in_a_QFileSystemModel
And don't forget to let the model emit signals (columnAdde/Remove etc.) afterwards to have the view catch up the changes.
-
you needn't emit signals if you just filter out rows. There are callback for hiding rows/columns in QSortFilterProxyModel: "filterAcceptsColumn":http://doc.qt.nokia.com/latest/qsortfilterproxymodel.html#filterAcceptsColumn and "filterAcceptsRow":http://doc.qt.nokia.com/latest/qsortfilterproxymodel.html#filterAcceptsRow
Also see the "example in the description of QSortFilterProxyModel":http://doc.qt.nokia.com/latest/qsortfilterproxymodel.html#filtering
-
Yep. But if the filter rules change after the view has finished displaying items, you must inform the view of the change. Fortunately the Trolls have provided us with slot "invalidateFilter() ":http://doc.qt.nokia.com/latest/qsortfilterproxymodel.html#invalidateFilter for this :-)
-
Ok, if I do this
// unhide all hidden columns int rows = myDataTableModel()->rowCount(); for(int i=0; i<rows; i++) { ui.mySimDataTableView->setColumnHidden(i, false); }
before
ui.mySimDataTableView->setModel(myDataTableModel);
everythink is fine.
Thank you :-)
-
[quote author="js_dev" date="1296216104"]Thanks so far.
I will have a look at the proxy model next week and will also try to unhide the hidden columns before I hide them again.[/quote]
If you use the proxy model approach, the setColumnHidden of the view is not needed anymore.
-
Ok, if I do this
// unhide all hidden columns int rows = myDataTableModel()->rowCount(); for(int i=0; i<rows; i++) { ui.mySimDataTableView->setColumnHidden(i, false); }
before
ui.mySimDataTableView->setModel(myDataTableModel);
everythink is fine.
Thank you :-)[/quote]
What is a bit strange:
you iterate over
rowCount
and callsetColumnHidden
This does not fit! -
Oh man - I mixed up rows and columns again :-(
Sorry for the confuision.It must look like this:
// unhide all hidden columns int cols = myDataTableModel()->columnCount(); for(int i=0; i<cols; i++) { ui.mySimDataTableView->setColumnHidden(i, false); } ui.mySimDataTableView->setModel(myDataTableModel());
-
I found another (maybe easier) way to unhide all the previously hidden columns.
If I resize the columns to content after hiding the columns I can get rid of the loop I posted above and the table view shows all the columns I want to see.
Do it like this:
if (stuff.exec(s)) { while(stuff.next()) { hide = stuff.value(1).toInt() == 0; index = myDataTableModel();->fieldIndex(stuff.value(0).toString()); if(index >= 0) { ui.mySimDataTableView->setColumnHidden(index, hide); } } } // This will resize the columns to content and also unhide all previously hidden columns ui.mySimDataTableView->resizeColumnsToContents();