Important: Please read the Qt Code of Conduct -

[SOLVED] Persisiting QTreeView's header between model updates

  • I'm migrating my project from QTreeWidget to QtreeView. With a widget there's no problem with the header - it lives as long as the widget does, so I just save its geometry and state to settings when quitting app and restoring when the app is starting.

    With QTreeView, however, it seems that I can only set header by doing model->setHorizontalHeaderLabels, and it has to be done again after model()->clear(), i. e. after every model update. So I'd have to recreate header view and store / restore its state and geometry every time. Is there a better way? Am I missing something?

  • Lifetime Qt Champion


    You can implement headerData in your custom model to return the headers for your view to show

  • Hi, thanks for the quick reply. I'm afraid that won't help. What I meant is persisiting header columns sizes, sort order, that kind of states. That must be done on view level, not model.

  • Lifetime Qt Champion

    Sorry I misunderstood what you said, you can then react on the modelAboutToBeReset signal, store the information you want to keep, then on reset you restore them

  • Thanks, that's exactly what I was looking for!

Log in to reply