1

我有一个 textEdit 字段,我想在该字段中处理一些选定的文本(但不是它的格式)。

到目前为止,我将按钮连接到:

QtCore.QObject.connect(self.ui.mytext_button,QtCore.SIGNAL("clicked()"), self.mytext)

方法:

def mytext(s):
     return s.upper()

但是我如何告诉 Python s 是选定的文本?我知道这是 selectionStart()、selectionEnd() 的问题。以及如何将其更改为 mytext 返回的内容?我认为是 insertText() 的东西,但在这里我也迷失了细节。

4

1 回答 1

2

回答我自己的问题。在这里为 Python 新手发帖:

获取选中的文本:

cursor = self.ui.editor_window.textCursor()
textSelected = cursor.selectedText()

将文本插入编辑器。

self.ui.editor_window.append(s) 

append() 也有替代方法,用于将文本插入到原始文本中。
因此,要将选定的文本变为大写:

def mytext(self):
        cursor = self.ui.editor_window.textCursor()
        textSelected = cursor.selectedText()
        s = textSelected.upper()
        self.ui.editor_window.append(s)  
于 2013-03-04T18:54:30.250 回答