Hide text with QSyntaxHighlighter

Problem: I want to implement a text editing widget for text with additional tags. I would like some tags to be invisible in some cases, so that they do not distract the user.

Wednesday: I use PyQtand prefer to use QPlainTextWidgetand QSyntaxHighlighter.

Approach: With QSyntaxHighlighterI can set QTextCharFormatfor strings that match my requirements. QTextCharFormatgives me all font properties like size, colors, etc. , but: I did not find a way to hide the text or reduce its size to zero.

I do not want to remove or replace tags, as this will introduce much more code (copying should contain tags and without I can not use QSyntaxHighlighterto form the remaining text in accordance with the tags).

Update: So far, I have found an ugly hack. By setting QTextFormat :: FontLetterSpacing to a small value, the text will consume less and less space. Combined with transparent color, the text is something like invisible.

Problem: In my test, this only worked for letter distances of up to 0.016%. Below the interval, it is reset to 100%.

+5
source share
1 answer

QTextDocument. , setVisible. QTextCursor, . .

. . QTextCursor. , QTextEdits.

:

from PyQt5 import QtWidgets, QtGui

app = QtWidgets.QApplication([])

w = QtWidgets.QPlainTextEdit()
w.show()

t = QtGui.QTextCursor(w.document())
t.insertText('plain text')
t.insertBlock()
t.insertText('tags, tags, tags')
t.block().setVisible(False)

print(w.document().toPlainText())

app.exec_()
+3

All Articles