本文整理汇总了Python中PyQt5.QtGui.QTextCursor.selectedText方法的典型用法代码示例。如果您正苦于以下问题:Python QTextCursor.selectedText方法的具体用法?Python QTextCursor.selectedText怎么用?Python QTextCursor.selectedText使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PyQt5.QtGui.QTextCursor
的用法示例。
在下文中一共展示了QTextCursor.selectedText方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_indentMoreWithSelection
# 需要导入模块: from PyQt5.QtGui import QTextCursor [as 别名]
# 或者: from PyQt5.QtGui.QTextCursor import selectedText [as 别名]
def test_indentMoreWithSelection(self):
cursor = QTextCursor(self.document)
cursor.setPosition(1)
cursor.setPosition(6, QTextCursor.KeepAnchor)
self.assertEqual('oo\u2029ba', # \u2029 is paragraph separator
cursor.selectedText())
documentIndentMore(self.document, cursor, self.settings)
self.assertEqual(' foo\n bar\nbaz',
self.document.toPlainText())
示例2: slotCursorPositionChanged
# 需要导入模块: from PyQt5.QtGui import QTextCursor [as 别名]
# 或者: from PyQt5.QtGui.QTextCursor import selectedText [as 别名]
def slotCursorPositionChanged(self):
"""Called whenever the cursor position changes.
Highlights matching characters if the cursor is at one of them.
"""
cursor = self.edit().textCursor()
block = cursor.block()
text = block.text()
# try both characters at the cursor
col = cursor.position() - block.position()
end = col + 1
col = max(0, col - 1)
for c in text[col:end]:
if c in self.matchPairs:
break
col += 1
else:
self.clear()
return
# the cursor is at a character from matchPairs
i = self.matchPairs.index(c)
cursor.setPosition(block.position() + col)
# find the matching character
new = QTextCursor(cursor)
if i & 1:
# look backward
match = self.matchPairs[i - 1]
flags = QTextDocument.FindBackward
else:
# look forward
match = self.matchPairs[i + 1]
flags = QTextDocument.FindFlags()
new.movePosition(QTextCursor.Right)
# search, also nesting
rx = QRegExp(QRegExp.escape(c) + "|" + QRegExp.escape(match))
nest = 0
while nest >= 0:
new = cursor.document().find(rx, new, flags)
if new.isNull():
self.clear()
return
nest += 1 if new.selectedText() == c else -1
cursor.movePosition(QTextCursor.Right, QTextCursor.KeepAnchor)
self.highlight([cursor, new])
示例3: _get_textdoc
# 需要导入模块: from PyQt5.QtGui import QTextCursor [as 别名]
# 或者: from PyQt5.QtGui.QTextCursor import selectedText [as 别名]
def _get_textdoc(self, index):
"""Create the QTextDocument of an item.
Args:
index: The QModelIndex of the item to draw.
"""
# FIXME we probably should do eliding here. See
# qcommonstyle.cpp:viewItemDrawText
text_option = QTextOption()
if self._opt.features & QStyleOptionViewItem.WrapText:
text_option.setWrapMode(QTextOption.WordWrap)
else:
text_option.setWrapMode(QTextOption.ManualWrap)
text_option.setTextDirection(self._opt.direction)
text_option.setAlignment(QStyle.visualAlignment(
self._opt.direction, self._opt.displayAlignment))
self._doc = QTextDocument(self)
if index.parent().isValid():
self._doc.setPlainText(self._opt.text)
else:
self._doc.setHtml('<b>{}</b>'.format(html.escape(self._opt.text)))
self._doc.setDefaultFont(self._opt.font)
self._doc.setDefaultTextOption(text_option)
self._doc.setDefaultStyleSheet(style.get_stylesheet("""
.highlight {
{{ color['completion.match.fg'] }}
}
"""))
self._doc.setDocumentMargin(2)
if index.column() == 0:
marks = index.data(basecompletion.Role.marks)
if marks is None:
return
for mark in marks:
cur = QTextCursor(self._doc)
cur.setPosition(mark[0])
cur.setPosition(mark[1], QTextCursor.KeepAnchor)
txt = cur.selectedText()
cur.removeSelectedText()
cur.insertHtml('<span class="highlight">{}</span>'.format(
html.escape(txt)))