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

Impossible to setHorizontalHeader bg-color(QTableWidget)



  • I successfully changed so much stuff of my horisontalHeaderItems, but can't change it's colour, no error, program compiles and work but this two attempts have no effect:

    ui->tabWidMain->horizontalHeaderItem(0)->setBackgroundColor(grey);
    
    ui->tabWidMain->model()->setHeaderData(0,Qt::Horizontal,QBrush(QColor("red")),Qt::BackgroundRole);
    


  • If that function not working because of some bug or smth, maybe there is another way to change color of a header?



  • I tried now even change stylesheet of whole TableWidget, and it worked, all table becomes grey. But not vertical and horizontal headers. WTF?



  • This is a Windows-only "feature". The windows style ignores the background. You can test it by calling QApplication::setStyle(QStyleFactory::create("Fusion")); and you'll see the background will work



  • @VRonin said in Impossible to setHorizontalHeader bg-color(QTableWidget):

    This is a Windows-only "feature".

    Damn it! Spent about 2 hours to figure out what's wrong. iKnew i did nothing wrong!

    Fusion is interesting. But i need windows style because it allow imitate thick, continues vertical lines between the columns(since there is no such functionality from Qt):

    alt text

    That lines between Headers and cells... Not good man, not good.



  • Okey. Is this possible to make desired horizontalHeaderItem invisible or something? Function ->hide(); would be perfect(so then total background will appear and goal is reached).



  • Not tested but you could subclass QHeaderView with something like:

    class SkipHeaderView : piblic QHeaderView{
    Q_OBJECT
    Q_DISABLE_COPY(SkipHeaderView)
    public:
    SkipHeaderView(Qt::Orientation orientation, QWidget *parent = Q_NULLPTR)
    :QHeaderView(orientation,parent ){}
    const QVector<int>& sectionsToSkip() const {return m_sectionsToSkip;}
    void setSectionsToSkip(const QVector<int>& sections){m_sectionsToSkip=sections; update();}
    protected:
    void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const Q_DECL_OVERRIDE{
    if(!m_sectionsToSkip.contains(logicalIndex))
    QHeaderView::paintSection(painter,rect,logicalIndex);
    }
    private:
    QVector<int> m_sectionsToSkip;
    };
    

Log in to reply