1

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.

i know nothing
  • 951
  • 1
  • 10
  • 27
  • I tested your code and it works correctly – eyllanesc Sep 22 '17 at 11:27
  • That's odd. I omitted the fact that I use application-wide stylesheets. I will disable them and test again. – i know nothing Sep 22 '17 at 11:29
  • Yep, the stylesheet seems to be the issue. I'm updating the question. – i know nothing Sep 22 '17 at 11:33
  • Show an example of how you apply the stylesheet. – eyllanesc Sep 22 '17 at 11:40
  • I have placed some stylesheets and still working, you could place an example of the stylesheet you use. – eyllanesc Sep 22 '17 at 11:53
  • If I comment out the parts in question it works. It has to be the stylesheet. But I added it to the question. – i know nothing Sep 22 '17 at 11:59
  • 2
    As I said I added the following style: `"QHeaderView::section {" "background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #616161, stop: 0.5 #505050, stop: 0.6 #434343, stop:1 #656565);" "color: white;" "padding-left: 4px;" "border: 1px solid #6c6c6c;" "}"` and works: https://imgur.com/a/Eik1K, see firstname and id – eyllanesc Sep 22 '17 at 12:03

1 Answers1

0

How about using the model of the data to be in charge of that?

If the model of the data derives from QAbstractItemModel, you can change the headerData method of it. Something like:

QVariant MyDerivedModel::headerData(int section, Qt::Orientation orientation, int role) const
{
   //... things

   if(role == Qt::FontRole) 
   {
      //if the column (the section) is one of the "special" ones, set it to bold
      //return the desired QFont in bold
   }

}
pablo_worker
  • 1,042
  • 9
  • 26