I’d like to have a QTableWidget that has 2 ‘header’ rows. Essentially I’d like to have the top 2 rows of the table not scroll vertically. For example:
Header 1 | Header 2
__________________
Header 3 | Header 4
__________________
Data | Data
__________________
Data | Data
__________________
Data | Data
__________________
Data | Data
__________________
I’d like to prevent any of the 4 headers (first two rows) not scroll off the screen as the user scrolls down.
I don’t see anyway in Qt to add additional header rows or prevent scrolling of a single row. Maybe there is a tricky way to do this by having 2 actual tables and 1 of those tables having a single row which is a header?
I found a way to do this albeit in a somewhat obscure way. I did find a great example on how to do something similar, but with columns instead of header rows.
http://doc.qt.nokia.com/4.7-snapshot/itemviews-frozencolumn.html
I created two tables, one for the header rows and one for the data. I then hid the horizontal headers for the data table, set the margins/spacing for everything to 0. This squished the tables close enough together to look like a single table.
Make sure to hide the horizontal scroll bars for each individual table and then adding a new scrollbar that connected the two hidden scroll bars. So, when the user scrolls with the stand-alone scrollbar it triggers events on the ‘real’ hidden scroll bars. This way users can only have a single scrollbar to interact with.
I also had to catch all the signals from the QHeaderView class
and make sure to apply the changes requested by the signals to both tables at the same time.
The trickest part was making sure the width of the vertical header items were the same length in both tables. The width of a vertical header item gets set on an event called resizeEvent, http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qwidget.html#resizeEvent. So I had to override this method in my class to set the headers on both tables to the same width.
Code:
Code: