coyoteazul
31st May 2016, 17:15
Hi. I've reimplemented QHeaderView to rotate text on the vertical header of my table (a QStandardItem through a QSortProxyModel)
Whenever i scroll to the sides, the painting goes wrong. Like it couldn't paint the new column headers. Also if i do something to fire the paint event, it only paints right if i'm on the initial position. How can i get it to paint properly?
Thanks in advance for your help.
Initial position
http://image.prntscr.com/image/2893c3e7470f4a2485c3fc8c692c66ad.png
Scroll to the furthest right
http://image.prntscr.com/image/94f4138006b94d709359ed8001fa3ffe.png
Scroll back to initial position
http://image.prntscr.com/image/0c7727efa0df447a8a75b53f50ab270e.png
This is my reimplementation
#include <QObject>
#include <QHeaderView>
#include <QPainter>
class QHeaderViewR : public QHeaderView
{
QStringList heads;
public:
QHeaderViewR(QStringList _heads):QHeaderView(Qt::Horizontal)
{
heads = _heads;
this->setSectionResizeMode(QHeaderView::Fixed);
this->setSectionsClickable(true);
}
void paintSection(QPainter * painter, const QRect & rect, int logicalIndex) const
{
QPen pen(Qt::black);
painter->setPen(pen);
painter->rotate(90);
painter->translate(0,-rect.width()+1);
QRect copy = rect;
copy.setWidth(rect.height());
copy.setHeight(rect.width());
copy.moveTo(0,-this->sectionPosition(logicalIndex));
if (logicalIndex == 0) //Show a line on the left
{
copy.setHeight(rect.width()-1);
painter->drawText(copy,Qt::AlignCenter, heads.at(logicalIndex));
}
else
{
painter->drawText(copy,Qt::AlignCenter ,heads.at(logicalIndex));
}
painter->drawRect(copy);
}
signals:
void sectionPressed(int logicalIndex());
};
Whenever i scroll to the sides, the painting goes wrong. Like it couldn't paint the new column headers. Also if i do something to fire the paint event, it only paints right if i'm on the initial position. How can i get it to paint properly?
Thanks in advance for your help.
Initial position
http://image.prntscr.com/image/2893c3e7470f4a2485c3fc8c692c66ad.png
Scroll to the furthest right
http://image.prntscr.com/image/94f4138006b94d709359ed8001fa3ffe.png
Scroll back to initial position
http://image.prntscr.com/image/0c7727efa0df447a8a75b53f50ab270e.png
This is my reimplementation
#include <QObject>
#include <QHeaderView>
#include <QPainter>
class QHeaderViewR : public QHeaderView
{
QStringList heads;
public:
QHeaderViewR(QStringList _heads):QHeaderView(Qt::Horizontal)
{
heads = _heads;
this->setSectionResizeMode(QHeaderView::Fixed);
this->setSectionsClickable(true);
}
void paintSection(QPainter * painter, const QRect & rect, int logicalIndex) const
{
QPen pen(Qt::black);
painter->setPen(pen);
painter->rotate(90);
painter->translate(0,-rect.width()+1);
QRect copy = rect;
copy.setWidth(rect.height());
copy.setHeight(rect.width());
copy.moveTo(0,-this->sectionPosition(logicalIndex));
if (logicalIndex == 0) //Show a line on the left
{
copy.setHeight(rect.width()-1);
painter->drawText(copy,Qt::AlignCenter, heads.at(logicalIndex));
}
else
{
painter->drawText(copy,Qt::AlignCenter ,heads.at(logicalIndex));
}
painter->drawRect(copy);
}
signals:
void sectionPressed(int logicalIndex());
};