0

I used QTextCharFormat to format a link style and insert into a QTextBrowser, when click this link, it show dotted outline (I thought this is its focus style). How to remove these dots when it's clicked?

Kha Tran
  • 193
  • 1
  • 1
  • 10

1 Answers1

2

Option A

If you don't want the QTextBrowser to gain focus at all, the simplest possible one-liner solution is to set its focusPolicy:

textBrowser->setFocusPolicy(Qt::NoFocus);

Notice that this is somewhat brutal approach, though, and prevents keyboard navigation altogether. In that respect, setting the focus policy to Qt::TabFocus is much nicer, but it won't prevent the focus rectangle to appear when QTextBrowser does have focus.

Option B

An alternative approach is to use a custom, or rather proxy, style to change the appearance of the focus indicator.

#include <QProxyStyle>

class MyProxyStyle : public QProxyStyle
{
public:
    int styleHint(StyleHint hint, const QStyleOption *option, const QWidget *widget, QStyleHintReturn *returnData) const
    {
        if (hint == SH_TextControl_FocusIndicatorTextCharFormat)
            return false;
        return QProxyStyle::styleHint(hint, option, widget, returnData);
    }
};

and then:

textBrowser->setStyle(new MyProxyStyle);

If you're interested in the implementation details to see how it works under the hood, see QWidgetTextControl::getPaintContext() and QCommonStyle::styleHint(). In short, the desired appearance is queried from the style, which can set the desired text format in the return data.

jpnurmi
  • 5,716
  • 2
  • 21
  • 37