I am writing a simple PyQt5 app, which uses QTableView
widget. What I want to do is to remove the dotted box around the focused item in QTableView
. For minimal working example I use this code:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, main_window):
main_window.setObjectName("main_window")
main_window.resize(640, 480)
self.main_container = QtWidgets.QWidget(main_window)
self.main_container.setObjectName("main_container")
self.verticalLayout = QtWidgets.QVBoxLayout(self.main_container)
self.verticalLayout.setObjectName("verticalLayout")
self.model = QtGui.QStandardItemModel(0, 2, main_window)
for col1, col2 in (("foo", "bar"), ("fizz", "buzz")):
it_col1 = QtGui.QStandardItem(col1)
it_col2 = QtGui.QStandardItem(col2)
self.model.appendRow([it_col1, it_col2])
self.view = QtWidgets.QTableView(
self.main_container, showGrid=False, selectionBehavior=QtWidgets.QAbstractItemView.SelectRows
)
self.view.setModel(self.model)
self.verticalLayout.addWidget(self.view)
main_window.setCentralWidget(self.main_container)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
It produces the following result (red arrow shows the box, which I want to remove):
Different topics on SO and Qt forums suggest using changing my code as:
# Previous code
self.view.setModel(self.model)
self.view.setStyleSheet(" QTableView::item:focus { outline: none } ") # also 0px was suggested
# OR
self.view.setModel(self.model)
self.view.setStyleSheet(" QTableView::item:focus { border: none } ") # also 0px was suggested
# OR
self.view.setModel(self.model)
self.view.setFocusPolicy(QtCore.Qt.FocusPolicy.NoFocus)
# Further code
But none of these approaches worked for me:
outline: none
approach changed literally nothing;border: none
not only keeps box, but also fills item with white;setFocusPolicy
works the best visually, but makes cells uneditable and generally unresponsive to keyboard events (which is expected, since they now can't be focused and accept events)
So my question is: is this possible to somehow remove or, at least, customize this box? If this matters, I'm using PyQt5==5.15.4
, PyQt5-Qt5==5.15.2
on Windows machine.