Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QTableView and horizontalHeader()->restoreState()

Tags:

qt

qtableview

I can't narrow down this bug, however I seem to have the following problem:

  • saveState() of a horizontalHeader()
  • 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.


2 Answers

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:

  • Store the state together with a list of the columns that existed in the model upon save, so you can avoid restoring from the data if the columns don't match, and revert to defualt case
  • Implement your own save/restoreState functions that handle that case (ugh)
  • Add a proxy model that has provides bogus/dummy columns for state that is being restored, then remove those columns just afterwards.
like image 145
Macke Avatar answered Nov 23 '25 20:11

Macke


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 that restoreState() (or equivalents) only ever worked for me when the model has already been set, and then some time. I ended up connecting to the QHeaderView::sectionCountChanged() signal and setting the state in the slot called from it.

like image 22
Marc Mutz - mmutz Avatar answered Nov 23 '25 18:11

Marc Mutz - mmutz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!