PDA

View Full Version : QTableView: saving/restoring columns' widths



mattc
17th December 2009, 19:50
Hello,
how can I save and restore the columns' widths of a QTableView? Basically, I want to store them in my QSettings and restore them on program start.

I already have the working code for saving and restoring the geometry of a QDialog, but it does not work for a QTableView.

I tried both:


myTableView->saveGeometry()
and

myTableView->horizontalHeader()->saveGeometry()

but then, when I call restoreGeometry() in the QTableView constructor, nothing happens.

Thank you

ChrisW67
17th December 2009, 22:05
Take a look at QHeaderView::saveState() and QHeaderView::restoreState(). This also stores column order and, I think, sort order.

I found that I could not restore the state in the constructor, presumably because the QHeaderView was not fully ready. In my case, I got around it by using a zero delay single-shot timer at the end of the constructor to trigger the restore when the event loop became idle. There is probably a more elegant way to do this:


// Schedule the restoration of the view state
// Done this way because if called directly by the constructor the
// QHeaderView is not fully constructed
QTimer::singleShot(0, this, SLOT(readSettings()));
If the column count can change between runs then be aware that restoring a mismatched state might cause problems. You may also need to force focus to the table view to make the columns reorder to match the header if column order is not fixed.