如何从 QTextEdit 或 QPlainTextEdit 小部件获取当前可见的文本?
这似乎是一件很常见的事情,但我找不到如何做。
我有一个带有一堆文本的 QTextEdit 或 QPlainTextEdit 小部件。足够了,滚动是必要的。
我想要另一个小部件提供有关当前可见文本的一些信息。为此,我需要知道
- 可见文本何时更改,
- 文本是什么?
我看到 QPlainTextEdit 有方法firstVisibleBlock,但它是受保护的。这告诉我,它实际上不应该在我的应用程序中使用。否则我不需要从编辑窗口进行子类化。
我还看到有信号 updateRequest 但不清楚我对 QRect 做了什么。
我该怎么做或者在哪里可以找到提示?
It seems like this would be a common thing to do, but I can't find how.
I have a QTextEdit or QPlainTextEdit widget with a bunch of text. Enough that scrolling is necessary.
I want another widget to give some information about the currently visible text. To do this, I need to know
- when the visible text changes
- what's the text?
I see that QPlainTextEdit has the method firstVisibleBlock, but it's protected. This tells me that it's not really something I should be using in my application. I wouldn't otherwise need to subclass from the edit window.
I also see that there's the signal updateRequest but it's not clear what I do with the QRect.
How do I do it or where do I find a hint?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我编写了一个包含两个 QTextEdit 字段的最小程序。您在左侧字段中写入,并且您正在写入的文本也会显示在第二个文本编辑中。您可以使用
toPlainText()
获取QTextEdit
的文本,信号为textChanged()
。我已经对其进行了测试,您在
m_pEdit_0
中编写的内容会在m_pEdit_1
中“实时”显示。main_window.hpp
main_window.cpp
main.cpp
编辑:
这也可以,但会缺乏性能巨大的文件:
I've written a minimal program that as two
QTextEdit
fields. In the left field you write and the text you are writing is shown in the second text edit too. You get the text of aQTextEdit
by usingtoPlainText()
and the signal istextChanged()
.I've tested it and what you write in
m_pEdit_0
is shown in "real-time" inm_pEdit_1
.main_window.hpp
main_window.cpp
main.cpp
Edit:
This would work too, but would lack in performance for huge documents: