0

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):

Undesired result

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.

  • 1
    Use an item-delegate, as shown in [this answer](https://stackoverflow.com/a/70368919/984421). – ekhumoro Feb 08 '23 at 19:23
  • Sorry for duplication, somehow it happened that I didn't try that answer from that thread, although I saw that thread. Works perfectly. I'm not really expertised member of SO community, should I delete the question beeing a duplicate now? – Евгений Крамаров Feb 09 '23 at 00:11
  • No problem. If a question is closed as a duplicate that isn't usually a bad thing. The main purpose of it is to link questions together so that people can find things more easily. It's not always easy to think of exactly the right search terms. Someone could indirectly find what they want via your question, so it's perhaps best not to delete it. – ekhumoro Feb 09 '23 at 02:58

0 Answers0