pythonqtpython-3.xpyqt4

How to change the characters used for QTextOption.ShowTabsAndSpaces?


Is there a way to change which character is used for QT's QTextOption.ShowTabsAndSpaces flag?

I find that the default character that's used for viewing whitespace (specifically spaces) stands out a little too much. I'd like to change the font or character used so that it's less distinct.

It looks like the character used is unicode "Middle Dot", · (U+00B7) and I'd like to use, say, U+02D1 ˑ.

Ideally I'd like to be able to set it to whatever the user wants.

I've been searching through the Qt docs and have only been able to find how to turn this flag on (here).

EDIT:

I guess I should show some code... Here's how I'm currently adding the whitespace indicators:

opts = self.document().defaultTextOption()
opts.setFlags(opts.flags() | QTextOption.ShowTabsAndSpaces)
self.document().setDefaultTextOption(opts)

Running Python 3.4 and PyQt4, but should be able to port C++ code over.

EDIT2:

Thanks to Andrei Shikalev's answer below, I've posted a feature request for this on the QT tracker: https://bugreports.qt.io/browse/QTBUG-46072


Solution

  • Currently we could not change characters for tabs and white space. This characters hardcoded in Qt source for QTextLayout:

    QChar visualTab(0x2192);
    ...
    QChar visualSpace((ushort)0xb7);
    

    More info in source for QTextLayout on GitHub.

    You can create feature request for tabs and white spaces custom characters. IMHO this feature will be useful for custom-looking editors, based on Qt.