目錄
文本光標
1、 理論基礎
- 通過文本光標,可以操作編輯文本文檔對象
- 概念:
- 整個文本編輯器,其實就是為編輯這個文本文檔,提供了一個可視化的界面
- 簡單理解,可以比喻成一個doc文檔,使用word軟件打開了這個文檔,就可以隨意修改文檔內容
- 獲取文本文檔的方法:
document()
:得到QTextDocument
textCursor()
:
2、 內容相關
2.1 添加內容
2.1.1 插入文本
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# @author: kun
from PyQt5.Qt import *
import sys
app = QApplication(sys.argv)
w = QWidget()
w.resize(500, 500)
tx = QTextEdit(w)
tx.resize(500, 500)
# 使用文本光標設置樣式
text_cursor = tx.textCursor() # 獲取文本光標對象
text_cursor.insertHtml("<h1>這是一級標題<h2>") # 插入富文本
text_cursor.insertText("這是普通文本內容") # 插入普通文本內容
format_ = QTextCharFormat() # 創建一個文本格式
format_.setToolTip("你好呀,你看到這句話,就說明語法沒關系")
format_.setFont(QFont("隸書")) # 設置文本字體為隸書
text_cursor.insertText("格式化文本", format_) # 設置文本的格式
w.show()
sys.exit(app.exec_())
2.1.2 插入圖片
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# @author: kun
from PyQt5.Qt import *
import sys
app = QApplication(sys.argv)
w = QWidget()
w.resize(500, 500)
tx = QTextEdit(w)
tx.resize(500, 500)
text_cursor = tx.textCursor() # 獲取文本光標對象
format_ = QTextImageFormat() # 創建一個圖片格式
format_.setName("./open.jpg") # 設置圖片
format_.setHeight(100) # 設置圖片高度
format_.setWidth(100) # 設置圖片寬度
text_cursor.insertImage(format_) # 添加圖片,推薦,其他創建方式可以到源代碼中查看
w.show()
sys.exit(app.exec_())
2.1.3 插入句子
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# @author: kun
from PyQt5.Qt import *
import sys
app = QApplication(sys.argv)
w = QWidget()
w.resize(500, 500)
tx = QTextEdit(w)
tx.resize(500, 500)
text_cursor = tx.textCursor()
qtd = QTextDocumentFragment.fromHtml("<h1>xxx</h1>") # 插入 HTML 文本
text_cursor.insertFragment(qtd)
w.show()
sys.exit(app.exec_())
2.1.4 插入列表
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# @author: kun
from PyQt5.Qt import *
import sys
app = QApplication(sys.argv)
w = QWidget()
w.resize(500, 500)
tx = QTextEdit(w)
tx.resize(500, 500)
text_cursor = tx.textCursor()
# text_cursor.insertList(QTextListFormat.ListDisc) # 插入列表,里面傳入列表的樣式
# text_cursor.createList(QTextListFormat.ListDisc) # 創建列表,里面傳入列表的樣式
format_ = QTextListFormat()
format_.setIndent(4) # 設置縮進
format_.setNumberPrefix("<<") # 設置列表前面圖標的前綴
format_.setNumberSuffix(">>") # 設置圖標的后綴
format_.setStyle(QTextListFormat.ListDecimal) # 設置圖標樣式
text_cursor.insertList(format_) # 指定格式插入列表
w.show()
sys.exit(app.exec_())
QtextListFormat.Style
:定義左側顯示的圖標
QTextListFormat.ListDisc
:圓圈QTextListFormat.ListCircle
:空圓圈QTextListFormat.ListSquare
:方塊QTextListFormat.ListDecimal
:十進制值按照升序排列QTextListFormat.ListLowerAlpha
:小寫拉丁字符按字母順序排列QTextListFormat.ListUpperAlpha
:大寫拉丁字符按字母順序排列QTextListFormat.ListLowerRoman
:小寫羅馬數字QTextListFormat.ListUpperRoman
:大寫羅馬數字
2.1.5 插入表格
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# @author: kun
from PyQt5.Qt import *
import sys
app = QApplication(sys.argv)
w = QWidget()
w.resize(500, 500)
tx = QTextEdit(w)
tx.resize(500, 500)
text_cursor = tx.textCursor()
format_ = QTextTableFormat()
format_.setAlignment(Qt.AlignRight) # 列表向右對齊
format_.setCellPadding(6) # 設置內邊距
format_.setCellSpacing(0) # 設置外邊距
format_.setColumnWidthConstraints([QTextLength(QTextLength.PercentageLength, 4), QTextLength(QTextLength.PercentageLength, 6)])# 設置列寬約束,約束第一列列寬為 4 ,第二列列寬為 6
# text_cursor.insertTable(3, 2) # 創建一個三行二列的表格
table = text_cursor.insertTable(3, 2, format_) # 創建一個三行二列的表格,並且設置格式
table.appendRows(3) # 追加三行
table.insertRows(0, 3) # 在首行插入三行
w.show()
sys.exit(app.exec_())
2.1.6 插入文本塊
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# @author: kun
from PyQt5.Qt import *
import sys
app = QApplication(sys.argv)
w = QWidget()
w.resize(500, 500)
tx = QTextEdit(w)
tx.resize(500, 500)
tx.setFocus()
text_cursor = tx.textCursor()
format_ = QTextBlockFormat()
format_.setIndent(2) # 設置縮進
format_.setAlignment(Qt.AlignLeft) # 設置為右對齊
format_.setTopMargin(100) # 設置文本塊離頂層的外邊距
text_cursor.insertBlock(format_) # 插入一個文本塊,后面還可以添加文本字符格式
w.show()
sys.exit(app.exec_())
2.1.7 插入框架
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# @author: kun
from PyQt5.Qt import *
import sys
app = QApplication(sys.argv)
w = QWidget()
w.resize(500, 500)
tx = QTextEdit(w)
tx.resize(500, 500)
tx.setFocus()
text_cursor = tx.textCursor()
format_ = QTextFrameFormat()
format_.setBorder(10) # 設置邊框
format_.setBorderBrush(QColor("red")) # 設置顏色
text_cursor.insertFrame(format_) # 創建一個文本框架
w.show()
sys.exit(app.exec_())
2.2 設置和合並格式
text_cursor.setBlockCharFormat(QTextCharFormat) # 設置要格式化的當前塊,或選中包含的所有塊 的塊 char 格式
setBlockFormat(QTextBlockFormat) # 設置當前的塊格式,或選中包含的所有塊,進行格式化
setCharFormat(QTextCharFormat) # 將光標當前字符格式設置為指定格式;如果光標有選擇,則將選擇內容設置為指定格式
mergeBlockCharFormat(QTextBlockCharFormat) # 合並當前塊的char格式
mergeBlockFormat(QTextBlockFormat) # 合並當前塊的格式
mergeCharFormat(QTextCharFormat) # 合並當前字符格式
合並的效果是疊加的
2.3 獲取內容和格式
block() # 獲取光標所在的文本塊
block().text() # 獲取光標的文本內容
blockFormat() # 獲取光標所在的文本塊格式
blockCharFormat() # 獲取光標所在文本塊的字符格式
blockNumber() # 獲取光標所在文本塊的編號
charFormat() # 獲取文本字符格式
currentFrame() # 獲取當前所在的框架
currentList() # 獲取當前所在的文本列表
currentTable() # 獲取當前所在的表格
2.4 文本選中和清空
2.4.1 光標選中
setPositon(int pos, QTextCursor.MoveMode=MoveAnchor) # 設置光標位置,反向設置
movePosition(QTextCursor.MoveOperation, QTextCursor.MoveMode=MoveAnchor, int n = 1) # 反向設置
select(QTextCursor.SelectionType) # 反向設置
QTextCursor.MoveMode
:
QTextCursor.MoveAnchor
: 將錨點移動到與光標本身相同的位置QTextCursor.KeepAnchor
: 將錨點固定值哎原來的位置
QTextCursor.MoveOperation
: 內容比較多,請到官方文檔查看
QTextCursor.SelectionType
:
QTextCursor.Document
: 選擇整個文檔QTextCursor.BlockUnderCursor
:選擇光標下的文本塊QTextCursor.LineUnderCursor
:選擇光標下的文本行QTextCursor.WordUnderCursor
:選擇光標下的單詞#!/usr/bin/env python # -*- coding: UTF-8 -*- # @author: kun from PyQt5.Qt import * import sys app = QApplication(sys.argv) w = QWidget() w.resize(500, 500) tx = QTextEdit(w) tx.resize(500, 400) def btn_(): text_cursor = tx.textCursor() # text_cursor.setPosition(6, QTextCursor.MoveAnchor) # 將光標移動到6的位置,設置錨點位置,錨點跟隨移動 # text_cursor.movePosition(QTextCursor.Up, QTextCursor.KeepAnchor, 2) # 光標向上移動2行,錨點不跟隨移動 text_cursor.select(QTextCursor.LineUnderCursor) tx.setTextCursor(text_cursor) # 需要把文本光標對象設置回給文本,這叫反向設置 tx.setFocus() btn = QPushButton("test", w) btn.pressed.connect(btn_) btn.move(203, 410) w.show() sys.exit(app.exec_())
2.4.2 選中內容獲取
selectText() # return str
selectionStart() # 獲取開始位置的索引
selectionEnd() # 獲取結束位置的索引
selection() # return QTextDocumentFragment
selectedTableCells() # return (int first_row_index, int rows_count, int first_col_index, int col_count)
示例:
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# @author: kun
from PyQt5.Qt import *
import sys
app = QApplication(sys.argv)
w = QWidget()
w.resize(500, 500)
tx = QTextEdit(w)
tx.resize(500, 400)
tx.textCursor().insertTable(5, 3) # 創建表格
def btn_():
text_cursor = tx.textCursor()
print(text_cursor.selectedText()) # 返回字符串
print(text_cursor.selectionStart())
print(text_cursor.selectionEnd())
print(text_cursor.selection().toPlainText()) # QTextDocumentFragment,輸出普通文本文檔
print(text_cursor.selectedTableCells()) # return (int first_row_index, int rows_count, int first_col_index, int col_count)
btn = QPushButton("test", w)
btn.pressed.connect(btn_)
btn.move(203, 410)
w.show()
sys.exit(app.exec_())
2.4.3 選定內容刪除和判定
clearSelection() # 取消選中的內容,其要反向設置
hasSelection() # 判斷有沒有選中內容
removeSelection() # 移除選定的內容,不需要反向設置
deleteChar() # 如果沒有選中文本,刪除文本光標后一個字符;選中文本,則刪除選中內內容
deletePreviousChar() # 如果沒有選中文本,刪除文本光標前一個字符;選中文本,則刪除選中內容
2.5 位置相關
atBlockEnd() # 是否在文本塊的末尾
atBlockStart() # 是否在文本塊的開始
atEnd() # 是否在文檔末尾
atStart() # 是否在文檔開頭
columnNumber() # 返回在第幾列
position() # 返回光標在文檔位置
positionBlock() # 返回在文本塊中的位置
2.6 開始和結束編輯標識
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# @author: kun
from PyQt5.Qt import *
import sys
app = QApplication(sys.argv)
w = QWidget()
w.resize(500, 500)
tx = QTextEdit(w)
tx.resize(500, 400)
def btn_():
text_cursor = tx.textCursor()
text_cursor.beginEditBlock() # 開始插入文本內容
text_cursor.insertText("hello")
text_cursor.insertText("world")
text_cursor.endEditBlock() # 結束插入文本內容
btn = QPushButton("test", w)
btn.pressed.connect(btn_)
btn.move(203, 410)
w.show()
sys.exit(app.exec_())
通過這個方法,可以將多個內容輸入合並為一個輸入,方便對內容的后續操作