I'm using application-wide stylesheets to alter my QTableView's look. At the same time, I want certain column headers to have bold text, depending on the header text. For this I derived from QHeaderView and implemented the paintSection function:
class BoldHeaderView : public QHeaderView
{
Q_OBJECT
public:
BoldHeaderView( Qt::Orientation orientation, QWidget* parent = 0 ) : QHeaderView( orientation, parent ) { }
void addBoldColumn( QString column_name )
{
if ( !m_bold_columns.contains( column_name ) )
m_bold_columns.append( column_name );
}
protected:
void paintSection( QPainter* p_painter, const QRect& rect, int logicalIndex ) const
{
QFont bold_font = p_painter->font();
QString column_name = model()->headerData( logicalIndex, Qt::Horizontal, Qt::DisplayRole ).toString();
if ( m_bold_columns.contains( column_name ) )
bold_font.setBold( true );
p_painter->setFont( bold_font );
QHeaderView::paintSection( p_painter, rect, logicalIndex );
}
private:
QList<QString> m_bold_columns;
};
Then I set this as the QTableView's horizontalHeader :
BoldHeaderView* p_bold_header = new BoldHeaderView( Qt::Horizontal );
p_bold_header->addBoldColumn( "Foo" );
m_p_table_view->setHorizontalHeader( p_bold_header );
My stylesheet looks like this:
QTableView QHeaderView::section {
font-family: "Segoe UI";
background-color: white;
border-style: none;
}
And it is applied application-wide in the main-function:
QApplication app(argc, argv);
[...]
app.setStyleSheet( style_sheet );
Thanks to eyllanesc I found out that this conflicts with the stylesheet. The bold font will always be overwritten with whatever is specified there. I need to find a way to combine both methods.