I can't narrow down this bug, however I seem to have the following problem:
saveState()
of ahorizontalHeader()
- restart app
- modify model so that it has one less column
restoreState()
- Now, for some reason, the state of the headerview is totally messed up. I cannot show or hide any new columns, nor can I ever get a reasonable state back
I know, this is not very descriptive but I'm hoping others have had this problem before.
For QMainWindow, the
save/restoreState
takes a version number. QTableView's restoreState() does not, so you need to manage this case yourself.If you want to restore state even if the model doesn't match, you have these options:
I personally never use
saveState()
/restoreState()
in any Qt widget, since they just return a binary blob anyway. I want my config files to be human-readable, with simple types. That also gets rid of these kind of problems.In addition,
QHeaderView
has the naughty problem thatrestoreState()
(or equivalents) only ever worked for me when the model has already been set, and then some time. I ended up connecting to theQHeaderView::sectionCountChanged()
signal and setting the state in the slot called from it.I'm attempting to fix this issue for Qt 5.6.2, after hitting the same issue. See this link for a Qt patch under review, which makes restoreState() handle the case where the number of sections (e.g. columns) in the saved state does not match the number of sections in the current view.
I would expect it to break if you change the model! Those functions save and restore private class member variables directly without any sanity checks. Try restoring the state and then changing the model.
Here is the solution I made using Boost Serialization.
It handles new and removed columns, more or less. Works for my use cases.