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?
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?