pyqt textbrowser用cursor选取文本中的若干行
时间: 2023-05-12 20:06:39 浏览: 72
可以使用以下代码实现:
```python
# 获取textbrowser中的文本
text = self.textBrowser.toPlainText()
# 获取选中的文本行数
start = self.textBrowser.textCursor().blockNumber()
end = self.textBrowser.textCursor().blockNumber()
# 遍历选中的文本行
for i in range(start, end+1):
# 获取文本行的开始和结束位置
block = self.textBrowser.document().findBlockByNumber(i)
startPos = block.position()
endPos = startPos + block.length() - 1
# 选中文本行
cursor = self.textBrowser.textCursor()
cursor.setPosition(startPos)
cursor.movePosition(QtGui.QTextCursor.EndOfLine)
cursor.movePosition(QtGui.QTextCursor.NextCharacter, QtGui.QTextCursor.KeepAnchor, endPos - startPos + 1)
selectedText = cursor.selectedText()
# 输出选中的文本行
print(selectedText)
```
这段代码可以获取 pyqt 中的 textbrowser 中选中的文本行,并输出到控制台。