问题
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):
How can I disable this "cursor"?
回答1:
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.
回答2:
#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:
This answer is both simple and correct, I think, and should be the accepted answer here as well: https://stackoverflow.com/a/2061871/2752221
I'm not sure what the right way to handle that on SO is. Perhaps this question should be marked as a dup of that other question; I don't know how to do that. Anyway, there's a link to a good answer, for those who read this far. :->
回答4:
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.
来源:https://stackoverflow.com/questions/24973378/how-to-disable-selection-highlighting-in-a-qtablewidget