如何在 QTextEdit 中设置文本块的可见性?

发布于 2024-08-29 19:48:26 字数 397 浏览 5 评论 0原文

我试图在 QTextEdit 中隐藏文本块,但它不起作用:

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

此代码适用于 QPlainTextEdit,但不适用于 QTextEdit。在文档中我没有找到任何关于它如何适用于 QTextEdit 的提及,只是如下:

void QTextBlock::setVisible(布尔可见) 设置块的可见性 可见。

该函数是Qt中引入的 4.4.

另请参见 isVisible()。

如何在 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 QTextBlock::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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜嗑 2024-09-05 19:48:26

FWIW,九年过去了,现在这似乎有效(Qt 5.13.1)。按照这个答案中的示例,但使用 C++:

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

当我显示文档时,我看到:

Hello world!

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!
情深已缘浅 2024-09-05 19:48:26

我已经确认了您所描述的行为。此外,我已经确认,在您给出的代码中,按照 setVisible 方法,块的可见性确实为 False。

所以,我看到的最清楚的解释是:QPlainTextEdit 不继承自 QTextEdit。它们都继承自 QScrollableArea,我只能假设 QTextEdit 不尊重其文档块的可见性。 QPlainTextEdit使用的文档使用QPlainTextLayout对象,而QTextEdit还有其他我无法确定的东西。

所以...我不确定它是否可以按照您想要的方式完成。一种替代方法是在文本进入 QTextEdit 之前过滤文本,Python 非常适合该任务。

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

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文