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开发者_如何学Python with some of the cells in the first row spanning multiple columns. Is this possible?
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.
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.
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
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.
I will write my solution, perhaps it is a crutch. But I redefined QHeaderView
and in it, instead of the standard cells, I used my own double ones. These double cells are essentially a QTableWidget
without a header.
class CHeaderView : public QHeaderView
{
Q_OBJECT
public:
CHeaderView(Qt::Orientation orientation, QWidget *parent = nullptr);
void showEvent(QShowEvent *e) override;
void SetHeight(const size_t& Height);
void AddCell(const CHeaderCellData&);
void AddCell(const QString& header, const QStringList& subHeaders = QStringList());
private slots:
void ResizeHeader();
private:
std::vector<CHeaderViewCell*> m_HeaderCells;
};
CHeaderViewCell::CHeaderViewCell(const int rowNumber, const int columnsNumber, QWidget* parent)
: m_CellData(new QTableWidget(rowNumber, columnsNumber, parent))
, m_Rows(rowNumber)
, m_Columns(columnsNumber)
{
m_CellData->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
m_CellData->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
m_CellData->setSelectionMode(QAbstractItemView::NoSelection);
m_CellData->setEnabled(false);
/* Hide Headers */
m_CellData->verticalHeader()->hide();
m_CellData->horizontalHeader()->hide();
m_CellData->horizontalHeader()->setMinimumSectionSize(25);
}
精彩评论