I'm using Qt4 and I have a QTableView
which displays data using a custom model. I'd like to have two rows in the table header, ideally with some of the cells in the first row spanning multiple columns. Is this possible?
问题:
回答1:
I too was looking for a solution to this problem. I've found this class: HierarchicalHeaderView at qt-apps.org by user Barmaglodd (Krasnoshchekov Petr).
That solution goes over and beyond what you (and I) need. Also, I cannot use their solution due to their copyright notice.
This post from blog.qt.digia.com by Andy Shaw shows you how to overlay QComboBoxes over a header. In my solution, I've done the same thing with another header view. This is similar to how they overlaid a QTableView on a QTableView for the Qt Frozen column example. For this solution, you do need to subclass both the QTableWidget and QHeaderView. Also, if you want to let the user adjust column width that takes extra work.
Tip: overload the SizeHint to give a height 2x of the normal height of the 'main' headerview, align the text of the main header view to the bottom and draw the other headerview on top of the normal one, do this in the
showEvent(QShowEvent* e)
method.
回答2:
I've had a similar problem regarding the multiple headder lines of a QTableView. I solved it using a simple "\n" in the column header text.
回答3:
You can make a custom header by making a QTableWidgetItem
and then using the setHorizontalHeaderItem()
. The method takes a column number, so I am not sure if that will allow for spanning multiple columns natively -- however, at the very least you could use the same QTableWidgetItem
for several columns.
As far as using two rows in the header, I do not believe this is currently supported. However, you could probably add this functionality by rolling your own derived class of QTableWidget
if you are ambitious.
回答4:
Under the impression of HierarchicalHeaderView I've made a Python port of it and a simple model to display pandas DataFrames with multilevel headers (MultiIndex) in QTableView.
Here're the first results dataframemodel. upd: it's quite slow with big DataFrames