Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QTreeView: change background color of specific column header



  • Is it possible to change the background color of a specific column of QTreeView header?

    This can change the background color of all columns:

    ui->tree->setStyleSheet("QHeaderView::section { background-color:red }");
    

    This is not working:

    ui->tree->model()->setHeaderData(col, Qt::Horizontal, QBrush(QColor("red")), Qt::BackgroundRole);
    

    Is there any ideas?

    Thank you!


  • Lifetime Qt Champion

    Hi,

    The style used to draw your application is free to ignore certain hints to follow the OS style they represent. That's why your second line may have no effect.



  • I tried to use default and Fusion styles:

    QApplication::setStyle(QStyleFactory::create("Fusion"));
    

    Style was changed.

    The header cell is empty for both styles as a result.
    2.png


  • Lifetime Qt Champion

    Please provide a minimal compilable example that reproduces this.



  • Hi!
    Problem solved by redefinition of QHeaderView::paintSection.
    Thank you!


Log in to reply