1
votes

I'm currently developing a cross-platform app (current developing it's under Ubuntu 18.10 and Windows 10) with PyQT 5.13.1 on Python 3.6/3.7. I'm using a QTableWidget that I've initialized in this way:

myTable = QTableWidget()
myTable.setSelectionBehavior(QAbstractItemView.SelectRows)
myTable.setSelectionMode(QAbstractItemView.SingleSelection)
myTable.setAlternatingRowColors(True)

At app's start, my table has two columns but is empty (no rows) and I've a button to add rows one by one. When I do this the first time, the first row is added and it shows with the first cell with a sort of light blue highlight (in both Ubuntu and Windows) and the second cell without it, as in this screenshot:

enter image description here

If I click on any of those cells, the entire row is selected and shows a blue background, accordingly with the setSelectionBehavior initialization. If I CTRL+Click that selected row, it got deselected and goes back with the first cell active/highlighted and the second not. My question is: is there a way to completely remove that highlight (what I'd prefer) or to extend it also to adjacent cell? I've read around that one possible solution wuold be to disable table's focus, but I wish to avoid such a solution.

Thank you all in advance!

1

1 Answers

1
votes

The currentIndex exists even when no index is selected, and by default it is highlighted. So to eliminate this behavior you can use a delegate to eliminate that behavior:

class Delegate(QtWidgets.QStyledItemDelegate):
    def initStyleOption(self, option, index):
        super().initStyleOption(option, index)
        option.state &= ~QtWidgets.QStyle.State_HasFocus
delegate = Delegate(myTable)
myTable.setItemDelegate(delegate)