Important: Please read the Qt Code of Conduct -

QTableWidget: disabling automatic selection of row header when a column is selected and viceversa

  • I 've searched everywhere I could before posting again. Probably I use the wrong key words:
    the default behaviour I've seen so far is that when you select a row header, all column sections in column header get selected too. Viceversa if you select a column by pressing its header section all row sections get selected.
    I'd need to avoid this behaviour and having just one row section selection when I select one row and only one column section when I select one column.
    Is it possible?

    these picture show the behaviour I dont want.

    I'd like only column 2 selected withou having row sections "One" "Two" "Three" selected.

    table col selection.png

    similarly here...

    table row selection.png

  • Lifetime Qt Champion

    Connect to QHeaderView::sectionSelected() or sectionPressed() and do the selection you want.
    It may also work when you override QTableView::selectColumn() and QTableView::selectRow()

  • @Christian-Ehrlicher thank you:

    I can't override those methods as they are not "virtual".

    int main(int argc, char *argv[])
        QApplication a(argc, argv);
                "QTableWidget { background-color: #000000; alternate-background-color: #222222; gridline-color: #AAAAAA; color: #FFFFFF; selection-background-color: #FFC000; selection-color: #AA00FF}"
                "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   }"
        MainWindow w;;
        return a.exec();
    class MainWindow : public QMainWindow
        MainWindow(QWidget *parent = nullptr);
    protected slots:
        void onColumnSelected(int);
        QMdiArea * mArea;
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        mArea = new QMdiArea();
        QMdiSubWindow * sw = new QMdiSubWindow();
        sw->resize(900, 600);
        QWidget * widget = new QWidget();
        QVBoxLayout * vlayout = new QVBoxLayout();
        widget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
        QTableWidget * table = new QTableWidget();
        QStringList verHeaderList({"One", "Two", "Three"});
        connect(table->horizontalHeader(), SIGNAL(sectionClicked(int)), this, SLOT(onColumnSelected(int)));
    void MainWindow::onColumnSelected(int index)
        qDebug() << __FUNCTION__ << "index " << index;

    onColumnSelected works correctly.

    1. how can I stop table from highlighting-applying selection-changing color of cells and header sections automatically
    2. how can I select-change color of cells and header section clicked in my onColumnSelected

    if I insert in MainWindow constructor this test code:

    table->setItemSelected( table->verticalHeaderItem(1), true);

    I see no change at all.

  • ok I 've found some solutions thou I have more problems

    disconnect(table->horizontalHeader(), SIGNAL(sectionPressed(int)),table, SLOT(selectColumn(int)));
    disconnect(table->verticalHeader(), SIGNAL(sectionPressed(int)),table, SLOT(selectRow(int)));

    then I reconnect to my slots which select no more than one row or column

    connect(table->horizontalHeader(), SIGNAL(sectionClicked(int)), this, SLOT(onColumnSelected(int)));
    connect(table->verticalHeader(), SIGNAL(sectionClicked(int)), this, SLOT(onRowSelected(int)));
    connect(table->horizontalHeader(), SIGNAL(sectionDoubleClicked(int)), this, SLOT(onColumnDoubleClicked(int)));
    connect(table->verticalHeader(), SIGNAL(sectionDoubleClicked(int)), this, SLOT(onRowDoubleClicked(int)));
    void MainWindow::onColumnSelected(int index)
        qDebug() << __FUNCTION__ << "index " << index;
    void MainWindow::onColumnDoubleClicked(int index)
        qDebug() << __FUNCTION__ << "index " << index;
    void MainWindow::onRowSelected(int index)
        qDebug() << __FUNCTION__ << "index " << index;
    void MainWindow::onRowDoubleClicked(int index)
        qDebug() << __FUNCTION__ << "index " << index;
    void MainWindow::onCellSelected(int r, int c)
        qDebug() << __FUNCTION__ << "row index " << r << " column index " << c;
    void MainWindow::onCDoubleClicked(int row, int col)
        qDebug() << __FUNCTION__ << "row index " << row << " column index " << col;

    I didn't find a way to disconnect single cell pressed thou.

  • @Black-Imp
    There are signals like cellClicked & cellPressed, though I don't see you connecting those signals to your slots.

    What do you mean by/why do you want to "disconnect single cell pressed"? Signals are disconnected in the normal way, via QObject::disconnect(). The signal is for the table as a whole, so you can't connect/disconnect individual cells. However, in your slot you can handle only those you wish to. Though I suspect you already know that, so not sure what you have in mind.

  • @JonB I'd need to disconnect both headers and cells from any slot which automatically higlights them.

    My desired behaviour would be:

    i click on a cell, it highlights and it becomes selected.

    i click on a header section - row or column - and it highlights with all the cells on that row or on that column.

    I don't want more than a row/column selected, I don't want more than a single cell selected unless I selected a row or a column.

    I'm trying some solutions but I can't find a good compromised:

    1 . first of all, if I disconnect as I did header selectionPressed from selectColumn/Row I can't find a way to highlight programmatically a header section without selecting it.
    2. If I call, in my own slots connected to those signals , selectColumn(index); I get the good effect which only one row/column can be actually selected thou you can highlight more than one when you keep mouse button presse and move, but to have this behaviour you must keep table with a selection mode which allows for multiple selection. this means you cannot call setSelectionMode(QAbstractItemView::SingleSelection);
    3. If I don't call setSelectionMode(QAbstractItemView::SingleSelection); table allows for multiple cell selection by clicking on different cells. I only want multiple selection by clicking headers. I've tried all MultiSelection, ExtendedSelection, ContiguousSelection but none of them allow me to do what I need and user is able to make a mess like this:
    undesired multiple selection.png
    4. I could then try to disconnect some signal which automatically highlights cells when clicking over them and try to highlight them programmatically. I can't understand what slot I should disconnect and how to manually higlight cells.

  • please you can close this thread. I've found a possible solution to my needs but I just have to solve a little last issue which I can't understand how to solve. I'd post a new thread with that question only, if possible

  • Lifetime Qt Champion

    @Black-Imp said in QTableWidget: disabling automatic selection of row header when a column is selected and viceversa:

    please you can close this thread

    Only you can do this - mark it as solved with the topic Tools.

  • @Christian-Ehrlicher ok thank you. I'll delete all and open a new one

  • Lifetime Qt Champion

  • @Black-Imp
    Please don't delete your topic, there is nothing wrong with your question and the discussions can always be useful to others reading it, even if you have found another approach now.

    At the bottom of the page you should see a Topic Tools button (because this is your topic), pick Mark as Solved from there. If you choose to open a new topic now, you might like to put in a one-line reference to this topic from there so people know there was a relationship.

  • @JonB ok thou there's nothing solved here

  • Lifetime Qt Champion

    @Black-Imp You still have problems?

Log in to reply