How to disable selection highlighting in a QTableWidget?
Asked Answered
N

4

12

I have a QTableWidget with a disabled setSelectionMode (QTableWidget::NoSelection) and the QTableWidgetItems I fill in don't have the Qt::ItemIsEditable flag.

Nevertheless, a cell that has been clicked gets some kind of cursor (the black line at the bottom in my case):

Highlighted cell

How can I disable this "cursor"?

Narceine answered 26/7, 2014 at 16:57 Comment(0)
N
15

Does this help?

QPalette palette = tableWidget->palette();
palette.setBrush(QPalette::Highlight,QBrush(Qt::white));
palette.setBrush(QPalette::HighlightedText,QBrush(Qt::black));
tableWidget->setPalette(palette);

To elaborate a bit: the appearance of the items is governed by the palette of the view which you can retrieve with the TableWidget::palette() method. Note that it is returned as const so you have get a copy, change it and then apply it by using setPalette. Note also that here I simply set the cell color to white and the text color to black, ideally you would set it specifically to the default cell colors (also available from the palette). Note finally that in my case the item still retained a different border from the default one which I didn't attempt to address here.

You can read more details about the various color definitions e.g. here (for Qt 4.8) http://qt-project.org/doc/qt-4.8/qpalette.html#ColorRole-enum

edit: some more sifting it seems that you should get rid of any border around a widget upon interaction (not selection) with it by setting the focus policy of the whole widget like this:

tableWidget->setFocusPolicy(Qt::NoFocus);

if this doesn't do the trick, then I am running rapidly out of ideas.

Nelidanelie answered 27/7, 2014 at 17:48 Comment(4)
I still get the black bottom line with this …Narceine
@TobiasLeupold I found one more lead and added it to the answer. As I say there, if this doesn't help, I am out of ideas.Nelidanelie
another addition: it seems that this thread reached the same conclusion: #2056205 but there it is cautioned that using the Qt::NoFocus loses you the ability to navigate with the keyboard which may or may not be an issue for youNelidanelie
palette.setBrush(QPalette::Highlight, palette.brush(QPalette::Base)); palette.setBrush(QPalette::HighlightedText, palette.brush(QPalette::Text));Dynamometry
D
31
tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
tableWidget->setFocusPolicy(Qt::NoFocus);
tableWidget->setSelectionMode(QAbstractItemView::NoSelection);

These statements will disable the selection of cells in a table.

Drupe answered 24/1, 2017 at 6:2 Comment(0)
N
15

Does this help?

QPalette palette = tableWidget->palette();
palette.setBrush(QPalette::Highlight,QBrush(Qt::white));
palette.setBrush(QPalette::HighlightedText,QBrush(Qt::black));
tableWidget->setPalette(palette);

To elaborate a bit: the appearance of the items is governed by the palette of the view which you can retrieve with the TableWidget::palette() method. Note that it is returned as const so you have get a copy, change it and then apply it by using setPalette. Note also that here I simply set the cell color to white and the text color to black, ideally you would set it specifically to the default cell colors (also available from the palette). Note finally that in my case the item still retained a different border from the default one which I didn't attempt to address here.

You can read more details about the various color definitions e.g. here (for Qt 4.8) http://qt-project.org/doc/qt-4.8/qpalette.html#ColorRole-enum

edit: some more sifting it seems that you should get rid of any border around a widget upon interaction (not selection) with it by setting the focus policy of the whole widget like this:

tableWidget->setFocusPolicy(Qt::NoFocus);

if this doesn't do the trick, then I am running rapidly out of ideas.

Nelidanelie answered 27/7, 2014 at 17:48 Comment(4)
I still get the black bottom line with this …Narceine
@TobiasLeupold I found one more lead and added it to the answer. As I say there, if this doesn't help, I am out of ideas.Nelidanelie
another addition: it seems that this thread reached the same conclusion: #2056205 but there it is cautioned that using the Qt::NoFocus loses you the ability to navigate with the keyboard which may or may not be an issue for youNelidanelie
palette.setBrush(QPalette::Highlight, palette.brush(QPalette::Base)); palette.setBrush(QPalette::HighlightedText, palette.brush(QPalette::Text));Dynamometry
I
2

The below solution worked for me:

tableWidget->setFocusPolicy(Qt::NoFocus);

But the problem is that, you can not work with keyboard for going up and down on the QTableWidget.

So I think that solution is not good.

Impulsive answered 4/12, 2017 at 19:34 Comment(0)
M
0

You could probably try to set selection transparent as a workaround and keep the focus:

QTableView::item::selected
{
    background-color: transparent;
}
Maxia answered 27/6, 2022 at 7:25 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.