【问题标题】:Copying selected text with QTextCursor使用 QTextCursor 复制选定的文本
【发布时间】:2015-02-25 00:34:37
【问题描述】:

这些是我拥有的小部件,

self.recipient = QTextEdit(self)
self.document = QTextDocument(self)
self.recipient.setDocument(self.document)
self.cursor = QTextCursor(self.document)

我想要做的是使用QTextCursor 将所选文本复制到我的QTextEdit 中。我试过函数selectedText(),但它给了我一个空字符串。这是我尝试打印它的方法:

print('%s' % (self.cursor.selectedText()))

【问题讨论】:

    标签: python copy selection pyside qtextcursor


    【解决方案1】:

    您需要从文本编辑中检索当前光标:

        cursor = self.recipient.textCursor()
        print('%s' % (cursor.selectedText()))
    

    但请注意,此光标只是一个副本。如果您对其进行更改,这些更改不会立即更新文本编辑。为此,您需要重置光标,如下所示:

        # make some changes to the cursor
        cursor.select(QtGui.QTextCursor.LineUnderCursor)
        # update the text-edit
        self.recipient.setTextCursor(cursor)
    

    【讨论】:

      猜你喜欢
      • 2013-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-04
      • 1970-01-01
      • 2012-12-07
      • 2014-04-21
      • 1970-01-01
      相关资源
      最近更新 更多