PDA

View Full Version : Can't paint in reimplemented QHeaderView



Tottish
26th February 2011, 18:17
Hi!
I'm having a subclass of QTableView and want to draw icons, the size of which I can freely adjust, in the Horizontal Header and also for the header to support stylesheets.

From googling around for a while I've been lead to understand that it is not possible without reimplementing the QHeaderView since this class does not support delegates.

My plan was/is to inherit QHeaderView into a new class (IconHeaderView) and Override the paintSection method in which I would first call QHeaderView::paintSection for it to paint exactly as it does now (with no visible text data in the "header-buttons") and after that, paint my icon on top of the button.
But I cant get it to work. Nothing visible seems to get drawn on top of the buttons (which render just fine, with or without stylesheets set) using this code:


void IconHeaderView::paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
{

QHeaderView::paintSection(painter, rect, logicalIndex);


QPixmap myPixMap(":images/icons/header/ERSicoBUSS.png");
QRect drawingRect;
drawingRect.setHeight(22);
drawingRect.setWidth(60);
drawingRect.moveCenter(rect.center());

painter->drawPixmap(drawingRect, myPixMap);

qDebug() << "done drawing pixmap";

}

Can anyone tell me what is wrong? I'm guessing there is something funny about the painter used since the exact same code works like a charm in the delegate used for the rest of the table-view.
Of course I am also open to different approaches as long as it satisfies my requirements: freely resizeble icons and stylesheet support.

Thanks a bunch!
/Tottish

EDIT: Hmm, it seems to work fine if I create a new QPainter on the viewport as so: QPainter myPainter(viewport()). Kind of solves my problem but could someone tell me why is this?

wysota
27th February 2011, 08:00
Try this:

void IconHeaderView::paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
{
painter->save();
QHeaderView::paintSection(painter, rect, logicalIndex);
painter->restore();

QPixmap myPixMap(":images/icons/header/ERSicoBUSS.png");
QRect drawingRect;
drawingRect.setHeight(22);
drawingRect.setWidth(60);
drawingRect.moveCenter(rect.center());

painter->drawPixmap(drawingRect, myPixMap);

qDebug() << "done drawing pixmap";

}

Tottish
27th February 2011, 10:18
Well that certainly did the trick!
So the painter object is being modified inside the QHeaderView::paintSection-event?

As always, my hat goes off to you wysota! Thanks!
/Tottish

deepal_de
17th March 2011, 05:12
i need to add vertical text to my Qtreeview header...
can u help me,
im new to QT,still learning

this is my code


#ifndef MYHEADERVIEW_H
#define MYHEADERVIEW_H

#include <QWidget>
#include <QHeaderView>
#include <QPainter>

class myheaderView : public QHeaderView
{
Q_OBJECT

public:
myheaderView(QWidget *parent = 0);
~myheaderView();

void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
{
painter->save();
QHeaderView::paintSection(painter, rect, logicalIndex);
painter->restore();

painter->setPen(Qt::black);
painter->setBrush(Qt::black);
painter->rotate(90);
painter->drawText(10,10,"TEXT");

}
};


QTreeView *p = new QTreeView(this);
myheaderView *pH = new myheaderView(p);
p->setProperty("pos",QPoint(10,10));

can you point me in the right direction ?

wysota
17th March 2011, 08:38
You can't just rotate the painter. You have to recalculate the coordinates as the painter rotates around its (0,0) point and not around the origin of the header section. You should translate the painter properly before drawing and then back after drawing.

deepal_de
21st March 2011, 08:56
Hello,
I managed to get the rotated text,but its not giving the expected output :(
Need to draw the text in all the columns in header.
the text disperse sometime... Can you help me to correct this,

void CustomHeader:aintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
{
painter->save();
QHeaderView:aintSection(painter, rect, logicalIndex);
painter->restore();

painter->save();
painter->translate(20,20/*rect.bottomLeft().x()+20,75*/);
painter->rotate(270);
painter->drawText(0,-(rect.width()*logicalIndex), "Text");
painter->restore();
}

thanks you..