Qt Table widget. How to set the meaning/title for vertical header and horizontal header together?
c++, qt, qtablewidget
Solution
I think that it is impossible with standard headers (QHeaderView) because of:
Note: Each header renders the data for each section itself, and does not rely on a delegate. As a result, calling a header's setItemDelegate() function will have no effect.
So you need forgot about it and disable it, you should implement your own headers(set your color, your text and so on), but I of course will help with meaning/title. I achieved this with next item delegate:
.h:
#ifndef ITEMDELEGATEPAINT_H
#define ITEMDELEGATEPAINT_H
#include <QStyledItemDelegate>
class ItemDelegatePaint : public QStyledItemDelegate
{
Q_OBJECT
public:
explicit ItemDelegatePaint(QObject *parent = 0);
ItemDelegatePaint(const QString &txt, QObject *parent = 0);
protected:
void paint( QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index ) const;
QSize sizeHint( const QStyleOptionViewItem &option,
const QModelIndex &index ) const;
QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
void setEditorData(QWidget * editor, const QModelIndex & index) const;
void setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const;
void updateEditorGeometry(QWidget * editor, const QStyleOptionViewItem & option, const QModelIndex & index) const;
signals:
public slots:
};
#endif // ITEMDELEGATEPAINT_H
But all these method is not very useful here, you can implement it yourself with some specific actions, I will show you main method - paint()
void ItemDelegatePaint::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if(index.row() == 0 && index.column() == 0)
{
QRect source1 = QRect(option.rect.topLeft(),option.rect.size()/2);
QRect source2 = QRect(option.rect.topLeft(),option.rect.size()/2);
painter->drawLine(option.rect.topLeft(),option.rect.bottomRight());
source1.moveTopLeft(source1.topLeft() += QPoint(source1.size().width(),0));
painter->drawText(source1,"agent reagent");
source2.moveBottomLeft(source2.bottomLeft() += QPoint(0,source2.size().height()));
painter->drawText(source2,"hallide ion");
}
else
{
QStyledItemDelegate::paint(painter,option,index);
}
}
This code shows main idea and it is not final version, but you should do all these specific things by yourself. Of course this approach is not very easy, you can just create picture and set it to cell, but in this case picture will not be good scalable. My code works normal if user will resize some headers. To prove, see screenshots with different sizes.
Problem
I know how to set text labels into row or column headers. But I want to do something like this: https://i.stack.imgur.com/eMM6U.jpg I don't find any info on how to do the thing inside the red circumference. I'm starting to believe this can't be done with QTableWidget. Thanks ;)