如何在 QTextEdit 中设置文本块的可见性? [英] How to set visibility for textblocks in QTextEdit?

查看:68
本文介绍了如何在 QTextEdit 中设置文本块的可见性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

此代码适用于 QPlainTextEdit,但不适用于 QTextEdit.在文档中,我没有发现任何关于它应该如何用于 QTextEdit 的提及,只是如下:

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可见 )设置块的可见性可见.

void QTextBlock::setVisible ( bool visible ) Sets the block's visibility to visible.

这个函数是在Qt中引入的4.4.

This function was introduced in Qt 4.4.

另见 isVisible().

See also isVisible().

如何在 QTextEdit 中隐藏块?

How can i hide block's in QTextEdit?

提前致谢

推荐答案

我已确认您描述的行为.此外,我已经确认,在您提供的代码中,按照 setVisible 方法,块的可见性确实为 False.

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.

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

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.

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

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)

这篇关于如何在 QTextEdit 中设置文本块的可见性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆