<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[QTableWidget: how to highlight headers section programmatically + changing cell custom widget style]]></title><description><![CDATA[<p dir="auto">following  <a href="https://forum.qt.io/topic/112548/qtablewidget-disabling-automatic-selection-of-row-header-when-a-column-is-selected-and-viceversa">this thread</a></p>
<p dir="auto">I''ve created a custom table by inheriting from QTableWidget, and I had to use custom headers too. .<br />
My requirements were these:</p>
<ol>
<li>No cluster of cells selectable</li>
<li>row selection without highlighting the column header</li>
<li>column selection without highlighting the row header</li>
<li>no multiple row selection</li>
<li>no multiple column selection</li>
<li>cells must contain a custom widget with a label inside.</li>
</ol>
<p dir="auto">I could achieve most of my objectives but i have the following issues:</p>
<p dir="auto">I can't find a way to highlight programmatically a section of a header (column and rows)</p>
<p dir="auto">cell selected have, as their widgets have no background color, they get the table selected background colors accordingly to stylesheet but I need to change widget labels foreground color as well.<br />
I set normal foreground color with</p>
<pre><code>"QTableWidget QLabel { color: #FFFFFF ; }
</code></pre>
<p dir="auto">I'd like to find a way to set in stylesheet a color to assign to labels when item is selected without doing it manually by assigning my widgets style sheet when the cell where they are is selected or unselected.</p>
<p dir="auto">this is the whole minimal code</p>
<p dir="auto">main.cpp</p>
<pre><code>#include "mainwindow.h"
#include &lt;QApplication&gt;
#include &lt;QTableWidget&gt;
#include &lt;QHeaderView&gt;
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    a.setStyleSheet(
        "QTableWidget { background-color: #000000; alternate-background-color: #222222; gridline-color: #AAAAAA; color: #FF00FF; selection-background-color: #FFC000; selection-color: #0000FF}"
        "QTableWidget QHeaderView { background-color: #00000000;}"
        "QTableWidget QHeaderView::section { background-color: #444444; color: #FFFFFF; border: none; border-style: none;}"
        "QTableWidget QHeaderView::section:checked { background-color: #FFC000; color: #000000;}"
        "QHeaderView::section:horizontal { border-right: 1px solid #AAAAAA; }"
        "QHeaderView::section:vertical { border-bottom: 1px solid #AAAAAA; }"
        "QTableWidget QTableCornerButton::section {  background: #00000000;  border: 0px   }"
        "QTableWidget QLabel { color: #FFFFFF; }"
        );

    MainWindow w;
    w.show();
    return a.exec();
}
</code></pre>
<p dir="auto">mainwindow.h</p>
<pre><code>#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include &lt;QMainWindow&gt;
#include &lt;QMdiArea&gt;
#include &lt;QTableWidget&gt;
#include &lt;QHeaderView&gt;


class CustomWidget : public QWidget
{
public:
    CustomWidget();
};

class ColumnHeader : public QHeaderView
{
public:
    ColumnHeader();
protected:
    void mousePressEvent(QMouseEvent *e) override;
    void mouseMoveEvent(QMouseEvent *e) override;
private:

};

class RowHeader : public QHeaderView
{
public:
    RowHeader();
protected:
    void mousePressEvent(QMouseEvent *e) override;
    void mouseMoveEvent(QMouseEvent *e) override;
private:
};

class CustomTable : public QTableWidget
{
    Q_OBJECT
public:
    CustomTable();
protected:
    void mousePressEvent(QMouseEvent * event) override;
protected slots:
    void onColumnSelected(int);
    void onRowSelected(int);
    void onCellSelected(int, int);

};

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
protected slots:

private:
    QMdiArea * mArea;
    CustomTable * table ;
};
#endif // MAINWINDOW_H

</code></pre>
<p dir="auto">mainwindow.cpp</p>
<pre><code>#include &lt;QTableWidget&gt;
#include &lt;QHeaderView&gt;
#include &lt;QMdiSubWindow&gt;
#include &lt;QBoxLayout&gt;
#include &lt;QSizePolicy&gt;
#include &lt;QCheckBox&gt;
#include &lt;QLabel&gt;
#include &lt;QDebug&gt;
#include &lt;QMouseEvent&gt;
#include "mainwindow.h"


CustomWidget::CustomWidget()
{
    QHBoxLayout * layout = new QHBoxLayout();
    QCheckBox * checkbox = new QCheckBox();
    QLabel * label = new QLabel("Pluto");
    //label-&gt;setStyleSheet("background-color: #700700;");
    layout-&gt;addWidget(checkbox);
    layout-&gt;addWidget(label);
    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    setLayout(layout);

}

ColumnHeader::ColumnHeader():QHeaderView(Qt::Horizontal)
{

}

void ColumnHeader::mousePressEvent(QMouseEvent *event)
{
    int index = logicalIndexAt(event-&gt;pos());
    qDebug() &lt;&lt; __FUNCTION__ &lt;&lt; "index " &lt;&lt; index ;
    emit(sectionPressed(index));
}

void ColumnHeader::mouseMoveEvent(QMouseEvent *event)
{
}

RowHeader::RowHeader():QHeaderView(Qt::Vertical)
{

}

void RowHeader::mousePressEvent(QMouseEvent *event)
{
    int index = logicalIndexAt(event-&gt;pos());
    qDebug() &lt;&lt; __FUNCTION__ &lt;&lt; "index " &lt;&lt; index ;
    emit(sectionPressed(index));
}

void RowHeader::mouseMoveEvent(QMouseEvent *event)
{
}

CustomTable::CustomTable()
{
    setAlternatingRowColors(true);
    ColumnHeader * ch = new ColumnHeader();
    setHorizontalHeader(ch);
    RowHeader * rh = new RowHeader();
    setVerticalHeader(rh);
    QStringList verHeaderList({"One", "Two", "Three"});
    setRowCount(verHeaderList.size());
    setColumnCount(5);

    setVerticalHeaderLabels(verHeaderList);
    verticalHeader()-&gt;setVisible(true);

    setCellWidget(1, 1, new CustomWidget());

    disconnect(horizontalHeader(), SIGNAL(sectionPressed(int)), this,  SLOT(selectColumn(int)));
    disconnect(verticalHeader(), SIGNAL(sectionPressed(int)), this, SLOT(selectRow(int)));
    setEditTriggers(QAbstractItemView::NoEditTriggers);
    setSelectionMode(QAbstractItemView::ExtendedSelection);
    connect(horizontalHeader(), SIGNAL(sectionPressed(int)), this, SLOT(onColumnSelected(int)));
    connect(verticalHeader(), SIGNAL(sectionPressed(int)), this, SLOT(onRowSelected(int)));
    connect(this, SIGNAL(cellClicked(int, int)), this, SLOT(onCellSelected(int, int)));

}

void CustomTable::mousePressEvent(QMouseEvent *event)
{
    if (event-&gt;buttons() == Qt::RightButton)
    { return; }
    clearSelection();
    QModelIndex idx = indexAt(event-&gt;pos());
    if (!idx.isValid())
    {
        // ...
    }
    else
    {
        selectionModel()-&gt;select(idx, QItemSelectionModel::Select);
        emit(cellClicked(idx.row(), idx.column()));
    }

}

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    mArea = new QMdiArea();
    setCentralWidget(mArea);

    QMdiSubWindow * sw = new QMdiSubWindow();
    sw-&gt;resize(900, 600);
    mArea-&gt;addSubWindow(sw);
    sw-&gt;show();

    QWidget * widget = new QWidget();
    QVBoxLayout * vlayout = new QVBoxLayout();
    widget-&gt;setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    widget-&gt;setLayout(vlayout);

    sw-&gt;setWidget(widget);

    table = new CustomTable();


    //connect(table, SIGNAL(cellClicked(int, int)), this, SLOT(onCellSelected(int, int)));
    //table-&gt;setItemSelected( table-&gt;verticalHeaderItem(1), true);
    table-&gt;show();


    //table-&gt;setSelectionMode(QAbstractItemView::SingleSelection);
    //table-&gt;selectRow(0);

    vlayout-&gt;addWidget(table);
}

MainWindow::~MainWindow()
{
}

void CustomTable::onColumnSelected(int index)
{
    qDebug() &lt;&lt; __FUNCTION__ &lt;&lt; "index " &lt;&lt; index;
    clearSelection();
    horizontalHeader()-&gt;clearSelection();
    QModelIndex  idx1 = model()-&gt;index(0,index);
    QModelIndex idx2 = model()-&gt;index(rowCount() - 1, index);
    QItemSelection selection(idx1, idx2);
    selectionModel()-&gt;select(selection, QItemSelectionModel::Select);

}

void CustomTable::onRowSelected(int index)
{
    qDebug() &lt;&lt; __FUNCTION__ &lt;&lt; "index " &lt;&lt; index;
    clearSelection();

    QModelIndex  idx1 = model()-&gt;index(index,  0);
    QModelIndex idx2 = model()-&gt;index(index, columnCount() - 1);
    QItemSelection selection(idx1, idx2);
    selectionModel()-&gt;select(selection, QItemSelectionModel::Select);

}

void CustomTable::onCellSelected(int r, int c)
{
    qDebug() &lt;&lt; __FUNCTION__ &lt;&lt; "row index " &lt;&lt; r &lt;&lt; " column index " &lt;&lt; c;
}


</code></pre>
<p dir="auto"><img src="https://ddgobkiprc33d.cloudfront.net/0dd5dcae-2b87-48d0-9c08-e020b482aadf.png" alt="custom table col selection.png" class=" img-fluid img-markdown" /><br />
Img 1: ok but I need to highlight horizontal section header as well (column header)</p>
<p dir="auto"><img src="https://ddgobkiprc33d.cloudfront.net/92702783-ddbd-489f-97bf-a975a20b6471.png" alt="custom table row selection.png" class=" img-fluid img-markdown" /><br />
Img 2: ok but I need to highlight vertial section header as well(row header)</p>
<p dir="auto"><img src="https://ddgobkiprc33d.cloudfront.net/f7706ace-7369-4bf8-be05-0d37bbe878e1.png" alt="custom table my widget cell selection.png" class=" img-fluid img-markdown" /><br />
Img 3: ok but I need to style label text color once for all when item containing that widget is selected</p>
<p dir="auto">thank you</p>
]]></description><link>https://forum.qt.io/topic/112950/qtablewidget-how-to-highlight-headers-section-programmatically-changing-cell-custom-widget-style</link><generator>RSS for Node</generator><lastBuildDate>Sun, 03 May 2026 00:14:19 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/112950.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 25 Mar 2020 23:46:31 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to QTableWidget: how to highlight headers section programmatically + changing cell custom widget style on Fri, 27 Mar 2020 20:11:00 GMT]]></title><description><![CDATA[<p dir="auto">no suggestion at all?</p>
]]></description><link>https://forum.qt.io/post/585195</link><guid isPermaLink="true">https://forum.qt.io/post/585195</guid><dc:creator><![CDATA[Black Imp]]></dc:creator><pubDate>Fri, 27 Mar 2020 20:11:00 GMT</pubDate></item></channel></rss>