View Full Version : QHeaderView's text rotation
MarkoSan
1st August 2014, 06:58
Dear Sirs and Madams!
I have subclassed QHeaderView, it has some QStringList as model. Now, I would like to rotate column names in QHeaderView. What do I have to do, I am a little bit lost...? I am attaching reference screenshot.
Sincerely,
Marko
10540
Rajesh.Rathod
21st August 2014, 07:33
Read QMatrix class it can be helpful to you for your requirement...
Here I have given some code hint for you....
QMatrix matrix;
matrix.rotate(angle);
pPainter->setWorldMatrix(matrix, true);
Thanks,
MarkoSan
21st August 2014, 08:23
Read QMatrix class it can be helpful to you for your requirement...
Here I have given some code hint for you....
QMatrix matrix;
matrix.rotate(angle);
pPainter->setWorldMatrix(matrix, true);
Thanks,
I suppose this code belongs in reimplemented paintSection, am I right maybe?
Rajesh.Rathod
21st August 2014, 11:20
Yes , you are right.
MarkoSan
21st August 2014, 11:52
I did that now:
void HeaderViewHorizontal::paintSection(QPainter* painter,
const QRect& rect,
int logicalIndex) const
{
QMatrix matrix;
painter->save();
matrix.rotate(45);
painter->setWorldMatrix(matrix);
painter->restore();
}
but now the header's texts are not shown at all.
Added after 7 minutes:
I've forgotten to call drawText(), here is upgrade:
void HeaderHorizontal::paintSection(QPainter* painter,
const QRect& rect,
int logicalIndex) const
{
QMatrix matrix;
// painter->save();
matrix.rotate(45);
painter->setWorldMatrix(matrix);
painter->drawText(this->rect(),
this->model()->headerData(logicalIndex,
Qt::Horizontal).toString());
// painter->restore();
}
but now I get all titles at first section. Screenshot attached.10572
Rajesh.Rathod
21st August 2014, 13:31
You need to create new rectangle and need to move it's centre before calling draw text...
int angle = 90;
QMatrix matrix;
matrix.rotate(angle);
painter->setWorldMatrix(matrix);
QRect new_r(0, 0, rect.height(), rect.width());
switch(angle)
{
case 90:
new_r.moveCenter(QPoint(rect.center().y(), -rect.center().x()-2));
break;
case -90:
default:
new_r.moveCenter(QPoint(-rect.center().y(), rect.center().x()));
break;
}
rect = new_r;
pHeaderView->style()->drawItemText(pPainter, new_r, Qt::AlignLeft,
uniopt.palette, true, uniopt.text);
For more code hint you can refer the code example given in link...
http://qt-apps.org/content/show.php/HierarchicalHeaderView?content=103154
and you should implement this code in paintVeritcalSection function of given example
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.