View Full Version : QHeaderView header width and selected background color
TheRonin
21st October 2013, 15:25
I have two questions that are both related to the QHeaderView for a QTableView:
1. How can I set the vertical header width? I can use the model to set the data to " " or some other character but i would prefer to set a fixed pixel width. Setting a minimum size for the horizontal header does not seem to have any effect on the vertical header's width.
2. I have given each vertical header a background color. This color is overridden when the row is selected, provided setClickable() has been set to True. How can i override the selected header item's background color using the QPalette?
Thank you for your time,
TR
spirit
21st October 2013, 18:13
You could try to use QSS, see Customizing QHeaderView (http://qt-project.org/doc/qt-4.8/stylesheet-examples.html#customizing-qheaderview).
TheRonin
21st October 2013, 18:35
I'll try that! If i can't use QSS, is there any other way?
ChrisW67
21st October 2013, 22:17
QHeaderView::setDefaultSectionSize() and QHeaderView::resizeSection() called on the verticalHeader() of the view. You might also want to set the resizeMode().
spirit
22nd October 2013, 08:12
For the first option you can use for instance:
...
tableView->verticalHeader()->setMinimumWidth(100);
...
The second option is a bit harder to implement. You will probably need to reimplement QHeaderView::paintSection.
Because of drawing section is based on a current style (QStyle) changing palette of a header can't give you expected result.
So, we don't you simply user QSS as I suggested before, it works fine for the second option.
TheRonin
24th October 2013, 10:18
Using QSS worked for controlling the selection color but with the Style i'm using i had to override the regular QHeaderView::section with a style-sheet as well.
In my case i need to vary the color depending on what state my application is in, so constantly switching the stylesheet might be ineffective in that regard. I managed to override paintSection per your recommendation and can thereby paint the section with a dynamic color. As you mentioned, i could not simply change the palette and had to draw the rectangle and text manually. It doesn't look as nice as I would like but might be good enough in this case. Here is the code:
class RowHeader : public QHeaderView
{
public:
void paintSection(QPainter* painter, const QRect& rect, int logicalIndex) const
{
QModelIndexList selectedRows = this->selectionModel()->selectedRows();
foreach (const QModelIndex& rowIndex, selectedRows){
if (rowIndex.row() == logicalIndex){
QBrush brush = qvariant_cast<QBrush>(rowIndex.data(Qt::BackgroundRole));
QString text = qvariant_cast<QString(rowIndex.data(Qt::DisplayRole));
painter->save();
painter->setBrush(brush.color().darker());
painter->drawRect(rect);
painter->restore();
painter->drawText(rect, Qt::AlignCenter, text);
return;
}
}
QHeaderView::paintSection(painter, rect, logicalIndex);
}
};
Thanks to everyone who responded to the thread!
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.