durden2.0 durden2.0 - 1 year ago 209
Python Question

Create QTableWidget with 2 or more header rows

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?

Answer Source

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.


  1. 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.

  2. 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.

  3. 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.

  4. 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.


def resize(self):
    Called when we know the data table has been setup by Qt so we are
    guaranteed that the headers now have a width, etc.

    There is no other way to guarantee that your elements have been sized,
    etc. by Qt other than this event.

    # Make the width of the vertical headers on the header table the same
    # size as the initialized width of the data table (data table widths
    # are setup automatically to fit the content)
    width = self._data_table.verticalHeader().width()
  1. Override the selectAll() method so when a user clicks the corner button, if enabled, all data in both tables will be selected:


def selectAll(self):
    """Select all data in both tables"""

    for table in [self._header_table, self._data_table]:
        for row in xrange(table.rowCount()):
            for col in xrange(table.columnCount()):
                item = table.item(row, col)
                table.setItemSelected(item, True)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download