1 Attachment(s)
QHeaderView paintSection reimplementation
I reimplemented QHeaderView so I could make it look more like my QTreeView.
I have got this code:
Code:
MusicListHeaderView
::MusicListHeaderView(QWidget* parent
): QHeaderView(Qt
::Horizontal, parent
){
hoverIndex = -1;
}
void MusicListHeaderView
::paintSection(QPainter* painter,
const QRect
& rect,
int logicalIndex
) const {
drawBackground(painter, rect, logicalIndex);
drawText(painter, rect, logicalIndex);
}
bool MusicListHeaderView
::event(QEvent* event
) {
switch(event->type())
{
{
int hoverIndex = logicalIndexAt(hover->pos());
}
break;
{
hoverIndex = -1;
}
break;
{
hoverIndex = logicalIndexAt(hover->pos());
}
break;
}
}
void MusicListHeaderView
::drawBackground(QPainter* painter,
const QRect
& rect,
int index
) const {
if(hoverIndex == index)
painter
->fillRect
(rect,
QColor(0xF0, 0xF0, 0xF0
));
else
painter->fillRect(rect, Qt::white);
painter
->setPen
(QColor(0xF0, 0xF0, 0xF0
));
painter->drawLine(rect.x(), rect.y()+rect.height()-1, rect.x()+rect.width(), rect.y()+rect.height()-1); // horizontal
// painter->drawLine(rect.x()+rect.width()-1, rect.y(), rect.x()+rect.width()-1, rect.y()+rect.height()); // vertical
}
void MusicListHeaderView
::drawText(QPainter* painter,
const QRect
& rect,
int index
) const {
painter->setPen(Qt::black);
painter
->setFont
(QFont("Corbel",
9,
QFont::Bold));
painter->drawText(rect.adjusted(10, 0, 10, 0), Qt::AlignLeft | Qt::AlignVCenter | Qt::TextSingleLine, final);
}
But now when I resize my column to the size where it gets smaller than the view, then the standard header is drawn (check attachment: upper right corner).
How can I fix this? Should I reimplement the whole paintevent?
Thanks in advance,
Gillis
Re: QHeaderView paintSection reimplementation
What are u setting the header view on ? is it a tableview ??
There can be some simpler way to do it...
Re: QHeaderView paintSection reimplementation
Yes there was indeed a simpler way to do it :). I made a custom style and reimplemented drawControl :)
Re: QHeaderView paintSection reimplementation
I guess it can be even simpler ,
from ur code i can make out the following - u need to subclass headerview so that u can underline it....
also u dont want that when u resize the column, a extra column shud appear.
May be you should look at QHeaderView::setStretchLastSectionbec as far as i understand, u want the tableview to behave as treeview... i had done somethng similar, so if u can explain, may be we can help