Removing dotted border without setting NoFocus in Windows PyQt

℡╲_俬逩灬. 提交于 2020-06-24 06:07:07

问题


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!


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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.




回答5:


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


来源:https://stackoverflow.com/questions/9795791/removing-dotted-border-without-setting-nofocus-in-windows-pyqt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!