使用 QTextCursor、QTextEdit 选择 textEdit 对象的文本 [英] Select text of textEdit object with QTextCursor, QTextEdit
问题描述
我有一个 textEdit 字段,我想处理该字段中的一些选定文本(但不是它的格式).
I have a textEdit field and I want to process some selected text within this field (but not the format of it).
到目前为止,我将按钮连接到:
So far, I connect the button with:
QtCore.QObject.connect(self.ui.mytext_button,QtCore.SIGNAL("clicked()"), self.mytext)
QtCore.QObject.connect(self.ui.mytext_button,QtCore.SIGNAL("clicked()"), self.mytext)
方法:
def mytext(s):
return s.upper()
但是我如何告诉 Python s 是选定的文本?我知道这与 selectionStart()、selectionEnd() 有关.以及如何将其更改为 mytext 返回的内容?我认为是 insertText() 的东西,但在这里我也迷失了细节.
But how do I tell Python that s is the selected text? I know that is something with selectionStart(), selectionEnd(). And how to change it to what mytext returns? I think is something with insertText(), but here I am also lost at the details.
推荐答案
回答我自己的问题.在这里发布给 Python 新手:
Answering my own question. Posting here for fellow Python noobs:
获取选中的文本:
cursor = self.ui.editor_window.textCursor()
textSelected = cursor.selectedText()
将文本插入回编辑器.
self.ui.editor_window.append(s)
也有 append() 的替代方法,用于将文本插入到原始文本中.
因此,要将选定的文本变为大写:
There are also alternatives to append(), for inserting the text into the original text.
So, to put a selected text into uppercase:
def mytext(self):
cursor = self.ui.editor_window.textCursor()
textSelected = cursor.selectedText()
s = textSelected.upper()
self.ui.editor_window.append(s)
这篇关于使用 QTextCursor、QTextEdit 选择 textEdit 对象的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!