QHeaderView header width and selected background color
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
Re: QHeaderView header width and selected background color
You could try to use QSS, see Customizing QHeaderView.
Re: QHeaderView header width and selected background color
I'll try that! If i can't use QSS, is there any other way?
Re: QHeaderView header width and selected background color
QHeaderView::setDefaultSectionSize() and QHeaderView::resizeSection() called on the verticalHeader() of the view. You might also want to set the resizeMode().
Re: QHeaderView header width and selected background color
For the first option you can use for instance:
Code:
...
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.
Re: QHeaderView header width and selected background color
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:
Code:
{
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!