Removing dotted border without setting NoFocus in Windows PyQt

前端 未结 5 1662
猫巷女王i
猫巷女王i 2021-02-04 02:18

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

5条回答
  •  一向
    一向 (楼主)
    2021-02-04 02:26

    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.

提交回复
热议问题