Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing dotted border without setting NoFocus in Windows PyQt

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:

Dotted Focus? Border

Here's an example of why this doesn't work.

  • I have a Non-Modal widget popup, think a lightbox for an image.
  • I want to detect a mousePressEvent outside of the widget and close the widget as a result. To do this, I should catch the focusOutEvent.
  • However, if a vast majority of widgets in my program are set as NoFocus (to remove the border issue), then I cannot catch the focusOutEvent because, you guessed it, they have no focus policy.

Here's another example:

  • I have a QTreeWidget that is subclassed so I can catch keyPressEvents for various reasons.
  • The QTreeWidget is also set as NoFocus to prevent the border. Because of this, however, the widget never has focus and therefore no keyPressEvents can be caught.
  • A workaround for this (kludgy, imo) is to use the widget's grabKeyboard class, which is dangerous if I forget to releaseKeyboard later. This is not optimal.

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!

like image 704
Cryptite Avatar asked Mar 20 '12 22:03

Cryptite


4 Answers

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.

like image 132
Desh__ Avatar answered Nov 11 '22 11:11

Desh__


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.

like image 40
jdi Avatar answered Nov 11 '22 11:11

jdi


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.

like image 1
alexisdm Avatar answered Nov 11 '22 12:11

alexisdm


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
non root privilege

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.

like image 1
Alopex Avatar answered Nov 11 '22 12:11

Alopex