I am trying to implement frozen (those that don't scroll when the rest of the grid is scrolled horizontally) columns in a QTreeView
- just like in Excel.
I tried to find some native Qt way to freeze columns, but the only thing I could find is a side-mention in QAbstractScrollArea::setViewportMargins
, as mentioned in this post. So I followed the suggestion in the answer to the post and overlaid another QTreeView looking at the same model on top. I.e. now I have 2 QTreeViews, one only showing the frozen columns and the other showing the rest of the columns with horizontal scrolling. I have subclassed the full view, so that the user doesn't have to know about the frozen view and only work with the full view.
However, now I am faced with a synchronization issue: all the changes made to the main view and its header need to be mirrored to the frozen view and its header respectively.
E.g. if I call mainView->header()->setStretchLastSection(false)
, the same call needs to be replicated on the frozen view's header. However, I have no notification when the method is called - DynamicPropertyChanged events are only called when setProperty
is called and not when individual setter is.
So, my questions are:
- Is there a way to implement frozen columns without having multiple views?
- How can I implement the synchronization between main and the frozen views, so that the frozen veiw would match everything done in the main view and its header?
Thanks a lot!
I had a similar problem QTableView. In my research I could't find another method to create a frozen table using a single view. Hence I implemented my own custom view which derives from QTableview. You can find it here:
header file:
cpp file
The way it works: