如何在 QTextEdit 中设置文本块的可见性?
我试图在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
FWIW,九年过去了,现在这似乎有效(Qt 5.13.1)。按照这个答案中的示例,但使用 C++:
当我显示文档时,我看到:
FWIW, nine years on, this now seems to be working (Qt 5.13.1). Following the example from this answer, but with C++:
When I display the document I see:
我已经确认了您所描述的行为。此外,我已经确认,在您给出的代码中,按照 setVisible 方法,块的可见性确实为 False。
所以,我看到的最清楚的解释是:QPlainTextEdit 不继承自 QTextEdit。它们都继承自 QScrollableArea,我只能假设 QTextEdit 不尊重其文档块的可见性。 QPlainTextEdit使用的文档使用QPlainTextLayout对象,而QTextEdit还有其他我无法确定的东西。
所以...我不确定它是否可以按照您想要的方式完成。一种替代方法是在文本进入 QTextEdit 之前过滤文本,Python 非常适合该任务。
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.