There are a few questions on SO about this, all of which seem to say that the only way to remove the dotted border is to set the focusPolicy on widget/item in question to NoFocu
Most styles delegate the drawing of the focus indicator to the QStyle::drawPrimitive
function with PE_FrameFocusRect
as the element to be drawn.
So you should be able to disable that globally with the following style class installed on the application instance:
class NoFocusProxyStyle : public QProxyStyle {
public:
NoFocusProxyStyle(QStyle *baseStyle = 0) : QProxyStyle(baseStyle) {}
void drawPrimitive(PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const {
if(element == QStyle::PE_FrameFocusRect) {
return;
}
QProxyStyle::drawPrimitive(element, option, painter, widget);
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
a.setStyle(new NoFocusProxyStyle);
...
PS: It doesn't work with QGtkStyle
for some widgets (buttons, combobox), so it might not work for Windows or Mac either.
Set outline: 0
of the desired object. Refer following example which sets it for a QTableView
.
QTableView
{
outline: 0;
}
Works for QAbstractItemView
inherited classes. (QTreeWidget, QTableWidget
etc). Surprisingly this CSS property is not mentioned in the QT Documentation. See QT Style Sheet Reference Documenation.
On OSX you can do
QWidget.setAttribute(QtCore.Qt.WA_MacShowFocusRect, False)
. Not sure about Win or Linux. You might have to do it through stylesheets.
You can do this with PyQt5:
class Style(QProxyStyle):
def drawPrimitive(self, element, option, painter, widget):
if element == QStyle.PE_FrameFocusRect:
return
super().drawPrimitive(element, option, painter, widget)
app.setStyle(Style())
The dotted border actually annoy me too. I google it many times, try about hundred times to solve it but with less success. Now I want to summary three typical way maybe you already know it, but let's make it more clear so you can understand what you truly need.
First QSS
Qss was claimed as most simply way to solve the problem.
Actually it work quiet well under the non root privilege, but under root it broke.
table.setStyleSheet("QTableView:{outline: 0;}")
non root privilege
root privilege
So if your application need a root privilege to run, the QSS
maybe doesn't suit your need.
Second it's FrameSheet/FrameShape
It looks will be effective, but it just work well under non root privilege like the above method.
table.setStyleSheet("QTableView:{outline: 0}")
table.setFrameShape(QtWidgets.QFrame.NoFrame)
Third NoFocusDelegate
inheritance.
This is a great method, it solve the problem privilege irrelevant
class NoFocusDelegate(QtWidgets.QStyledItemDelegate):
def paint(self, QPainter, QStyleOptionViewItem, QModelIndex):
if QStyleOptionViewItem.state & QtWidgets.QStyle.State_HasFocus:
QStyleOptionViewItem.state = QStyleOptionViewItem.state ^ QtWidgets.QStyle.State_HasFocus
super().paint(QPainter, QStyleOptionViewItem, QModelIndex)
table.setItemDelegate(NoFocusDelegate())
This method help me to get rid of the dotted border mystery, I hope it will be helpful to you too.