QText编辑。如何手动选择文本?
有类似 textEdit->textCursor()->selectionStart()
和 textEdit->textCursor()->selectionEnd()
的函数,但没有函数setSelectionStart
、setSelectionEnd
。
有没有办法手动选择文本的某些部分?
There are functions like textEdit->textCursor()->selectionStart()
and textEdit->textCursor()->selectionEnd()
, but there are no functions setSelectionStart
, setSelectionEnd
.
Is there any way to select some part of text manually?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这段代码使用 将光标移动到选择的起始位置
setPosition
,然后将其移动到选择的末尾,但通过指定MoveMode
作为第二个参数。最后一行将选择设置为在编辑控件内可见,因此如果您只想对所选文本进行一些操作,则应该跳过它。
另外,如果您没有确切的位置,
movePosition
很有帮助:您可以在 各种方式,例如向右一个单词或向下一行。This piece of code moves the cursor to the start position of the selection using
setPosition
, then moves it to the end of the selection, but leaves the selection anchor at the old position by specifying aMoveMode
as the second parameter.The last line sets the selection to be visible inside the edit control, so you should skip it if you just want to do some manipulations with the selected text.
Also, if you don't have the exact positions,
movePosition
is helpful: you can move the cursor in various ways, such as one word to the right or down one line.我遇到了类似的问题。
在Windows 10中,可能存在“拖动/移动”的错误。我们使用 QT_NO_DRAGANDDROP 作为编译器选项,这使得 QTextEdit 中的文本选择不再起作用。
解决方案:
参考:
现有的两个答案
QTextEdit:获取单词在鼠标指针下?
I encountered a similar problem.
In Windows 10, there might be a bug of 'drag/move'. We use QT_NO_DRAGANDDROP as a compiler option, which makes text selection in QTextEdit not work anymore.
Solution:
reference:
Two existing answers
QTextEdit: get word under the mouse pointer?
尝试使用:
Try to use: