PDA

View Full Version : Subclassing QHeaderView



T4ng10r
20th August 2011, 12:52
Hi,
I want to put QCheckBox and QComboBox in each column header.
Previously I was creating entire new View (based on QGridLayout) to achieve this.
Currently I'm subclassing QHeaderView, then add layout in constructor with parent to viewport(). In layout place widget and small layout for each column. But this is ... complicated. Is there any other easier solution?

wysota
20th August 2011, 13:23
What is the layout for?

T4ng10r
20th August 2011, 15:40
I want to make widget for each sections to fit in size. So I want to wait for signal sectionCountChange then add as many custom widget as count. Layout will place them according to their sizeHint (or which other size function) - layout will keep control of placing widget so they will overlap QHeaderView default sections. I won't need to keep eye on their placement by my self.

wysota
20th August 2011, 15:48
That's an incorrect approach. If you want to replace headers with widgets completely then subclass the view, hide the header, use QAbstractScrollArea::setViewportMargins() to make space over the viewport and then create a set of widgets, optionally put them in a layout and place over the viewport instead of the header view. You'll have to reimplement resizeEvent for the view to position your pseudo-header properly when the view is resized. Furthermore it is a good idea to monitor changes in widget sizes and resize the columns of the view appropriately.

A different approach would be to indeed subclass QHeaderView but not use any layouts. Instead attach to appropriate events and signals and position all the widgets manually using QHeaderView::sectionSize().

T4ng10r
20th August 2011, 16:15
That's what I was afraid - lots of small things to keep eye on.
Second approach - why layout is incorrect way to place widgets? If I will keep size of each widget as it should be (according to sectionSize()) then layout will do all placement work. In your suggestion - I will have to manually calculate all this what layout does. Layout is so much inefficient?

PS. Similar thing I would like to do with vertical header - this time to add four horizontally aligned widgets.

wysota
20th August 2011, 17:03
Second approach - why layout is incorrect way to place widgets?
Because then the layout takes control of widget sizes and position. What happens if you start scrolling your view horizontally or resize sections? How will you hide a portion of a widget or how will you move those widgets aroung, especially if the last section of the header has the Stretch resize mode? You'll have more work this way than when positioning widgets manually.

T4ng10r
20th August 2011, 20:11
Thanks.
Are there any examples or tutorials with subclassed QHeaderView?

wysota
20th August 2011, 20:20
No, I haven't seen any. But it's not difficult, I've done it myself once or twice.