如何禁用QTableWidget中的选择突出显示

时间:2021-08-18 14:44:51

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

我有一个带有禁用的setSelectionMode(QTableWidget :: NoSelection)的QTableWidget,而我填写的QTableWidgetItem没有Qt :: ItemIsEditable标志。

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

然而,单击的单元格会获得某种光标(在我的情况下,底部的黑线):

如何禁用QTableWidget中的选择突出显示

How can I disable this "cursor"?

如何禁用此“光标”?

3 个解决方案

#1


7  

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.

详细说明:项目的外观由视图的调色板控制,您可以使用TableWidget :: palette()方法检索该视图。请注意,它以const形式返回,因此您可以获取副本,更改它,然后使用setPalette应用它。另请注意,这里我只是将单元格颜色设置为白色,将文本颜色设置为黑色,理想情况下,您可以将其专门设置为默认单元格颜色(也可从调色板中获得)。最后请注意,在我的情况下,该项仍然保留了与我在此处未尝试解决的默认边框不同的边框。

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

您可以阅读有关各种颜色定义的更多详细信息,例这里(对于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.

如果这不能解决问题,那么我的想法很快就会出现。

#2


1  

#include <QTableWidget>



tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
tableWidget->setFocusPolicy(Qt::NoFocus);
tableWidget->setSelectionMode(QAbstractItemView::NoSelection);

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

这些语句将禁用单元格中表格的选择。

#3


0  

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.

但问题是,你无法使用键盘在QTableWidget上上下移动。

So I think that solution is not good.

所以我认为解决方案并不好。

#1


7  

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.

详细说明:项目的外观由视图的调色板控制,您可以使用TableWidget :: palette()方法检索该视图。请注意,它以const形式返回,因此您可以获取副本,更改它,然后使用setPalette应用它。另请注意,这里我只是将单元格颜色设置为白色,将文本颜色设置为黑色,理想情况下,您可以将其专门设置为默认单元格颜色(也可从调色板中获得)。最后请注意,在我的情况下,该项仍然保留了与我在此处未尝试解决的默认边框不同的边框。

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

您可以阅读有关各种颜色定义的更多详细信息,例这里(对于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.

如果这不能解决问题,那么我的想法很快就会出现。

#2


1  

#include <QTableWidget>



tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
tableWidget->setFocusPolicy(Qt::NoFocus);
tableWidget->setSelectionMode(QAbstractItemView::NoSelection);

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

这些语句将禁用单元格中表格的选择。

#3


0  

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.

但问题是,你无法使用键盘在QTableWidget上上下移动。

So I think that solution is not good.

所以我认为解决方案并不好。