开发者

How to set visibility for textblocks in QTextEdit?

i tried to hide textblock's in QTextEdit, but it doesn't work:

block = textedit.document().begin()
block.setVisible(False)

This code works fine for QPlainTextEdit, but not for QTextEdit. In documentation i haven't found any mention of how it should work for QTextEdit, just following:

void QTextBloc开发者_Go百科k::setVisible ( bool visible ) Sets the block's visibility to visible.

This function was introduced in Qt 4.4.

See also isVisible().

How can i hide block's in QTextEdit?

Thank you in advance


FWIW, nine years on, this now seems to be working (Qt 5.13.1). Following the example from this answer, but with C++:

QTextCursor cursor(&mDocument);
cursor.insertText("Hello world! ");
cursor.insertBlock();
cursor.insertText("Goodbye world! ");
cursor.block().setVisible(false);

When I display the document I see:

Hello world!


I've confirmed the behavior your describe. In addition, I've confirmed that, in the code you've given, following the setVisible method the block's visibility is indeed False.

So, the clearest explanation I see is this: QPlainTextEdit does not inherit from QTextEdit. They both inherit from QScrollableArea and I can only assume that QTextEdit does not respect the visibility of its document's blocks. The documents used by QPlainTextEdit use QPlainTextLayout objects, and QTextEdit has something else that I cannot determine.

So... I'm not sure it can be done in the way you are intending. One alternative is to filter the text before it gets into the QTextEdit, and Python is well-suited for that task.

self.paragraphs = ["First paragraph","Second Paragraph","Third Paragraph",]
self.display_text = '\n'.join(self.paragraphs[1:])
self.textedit.setText(self.display_text)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜