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 NoFocus. While this works as a temporary fix, this prevents further interaction with said widget/item in the realm of other necessary focusEvents.
Said border in question:
Here's an example of why this doesn't work.
Here's another example:
So then the question is, is there a way to remove this weird (mostly just ugly) dotted border without turning off focus for everything in my app? Thanks in advance!
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With