一、QTextCursor多行文本框文本光標插入文本
1.基本步驟
1.首先創建一個光標對象;
qtc = self.qte.textCursor()
2.通過光標插入文本,傳入上面的格式對象;
3.插入文本內容格式
2.代碼
from PyQt5.Qt import *
import sys
class Window(QWidget) :
def __init__(self) :
super().__init__()
self.setWindowTitle("QTextEdit - PyQt5中文網")
self.resize(600, 500)
self.func_list()
def func_list(self) :
self.func()
def func(self) :
self.qte = QTextEdit('111', self)
self.qte.move(100, 100)
self.qte.resize(250, 250)
self.qte.setStyleSheet('background-color:green')
self.qte.setFrameStyle(QFrame.Box | QFrame.Raised)
self.qte.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
self.qte.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded)
self.btn = QPushButton('按 鈕', self)
self.btn.move(120, 50)
self.btn.resize(70, 30)
self.btn.pressed.connect(self.text_cur)
# ==============內容設置=============== # 代碼分割線 - 開始
# 文本光標完成內容和格式設置
def text_cur(self) :
# 1.首先創建一個光標對象;2.通過光標插入文本,傳入上面的格式對象;# 3.插入文本內容格式
# self.qte.document() # 獲取文本文檔的方法,返回QTextDocument
# self.qte.textCursor() # cursor是直接操作鼠標的方法,textCursor是操作光標的
# 插入文本
# 1.首先創建一個光標對象
# qtc = self.qte.textCursor()
# # 2.插入文本內容格式
# tcf = QTextCharFormat() # 創建一個格式對象
# tcf.setToolTip('pyqt5中文網') # 設置提示文本
# tcf.setFontFamily('楷書') # 設置字體
# tcf.setFontPointSize(25) # 設置字體大小
# # 3.通過光標插入文本,傳入上面的格式對象
# qtc.insertText('www.PyQt5.cn' , tcf)
# qtc.insertHtml("<a href='https://www.pyqt5.cn'>PyQt5中文網</a>")
# 插入圖片
# 1.首先創建一個光標對象
# qtc = self.qte.textCursor()
# # 3.插入圖片內容格式
# tcf = QTextImageFormat()
# tcf.setName('aaa.png')
# tcf.setWidth(40)
# tcf.setHeight(40)
# # 2.通過光標插入文本,傳入上面的格式對象
# qtc.insertImage(tcf)
# # 插入文件
# 1.首先創建一個光標對象
# qtc = self.qte.textCursor()
# # 3.插入文件內容格式
# tcf = QTextDocumentFragment().fromHtml("<a href='https://www.pyqt5.cn'>PyQt5中文網</a>") # 插入富文本
# # tcf.fromHtml("<a href='https://www.pyqt5.cn'>PyQt5中文網</a>") # fromHtml有返回值,所以必須給個變量名,這樣寫就錯了
# # tcf = QTextDocumentFragment().fromPlainText("<a href='https://www.pyqt5.cn'>PyQt5中文網</a>") # 插入普通文本
# # 2.通過光標插入文本,傳入上面的格式對象
# qtc.insertFragment(tcf)
# 插入列表1
# 1.首先創建一個光標對象
# qtc = self.qte.textCursor()
# # 3.插入文本內容格式
# # tcf = QTextList()
# # tcf.count()
# # 2.通過光標插入文本,傳入上面的格式對象
# tcf = qtc.insertList(QTextListFormat.ListDecimal) # 返回一個QTextList對象 ListDecimal是一個枚舉值 可更換
# # qtc.createList(QTextListFormat.ListDecimal)
# print(tcf.item(3))
# # 插入列表2
# # 1.首先創建一個光標對象
# qtc = self.qte.textCursor()
# # 3.插入文本內容格式
# tcf = QTextListFormat()
# tcf.setNumberPrefix('$') # 列表前綴,必須要加上setStyle,設置為數字
# tcf.setIndent(2) # 縮進一個Tab
# tcf.setStyle(QTextListFormat.ListDecimal)
# # 2.通過光標插入文本,傳入上面的格式對象
# qtc.insertList(tcf)
# # 插入表格
# # 1.首先創建一個光標對象
# qtc = self.qte.textCursor()
# # 3.插入表格格式
# tcf = QTextTableFormat()
# tcf.setAlignment(Qt.AlignRight)
# tcf.setHeight(30)
# tcf.setWidth(50)
# tcf.setCellPadding(1.1)
# tcf.setCellSpacing(0.1)
# tcf.setColumnWidthConstraints()
# # 2.通過光標插入文本,傳入上面的格式對象
# qtc.insertTable(2, 3)
# # qtc.insertTable(2, 3, tcf) # 返回QTextTable,還可以追加后續操作,如下
# # ttt = qtc.insertTable(2, 3, tcf)
# # ttt.appendColumns(3) # 追加3列
# #
# # 插入文本快
# qtc = self.qte.textCursor()
# tcf = QTextBlockFormat()
# tcf.setAlignment(Qt.AlignRight)
# tcf.setRightMargin(50)
#
# tcf2 = QTextCharFormat()
# tcf2.setFontFamily('隸書')
# tcf2.setFontWeight(50)
#
# qtc.insertBlock(tcf,tcf2)
# self.qte.setFocus()
# 插入框架
qtc = self.qte.textCursor()
tcf = QTextFrameFormat()
tcf.setBorder(3)
tcf.setRightMargin(20)
qtc.insertFrame(tcf)
# ==============內容設置=============== # 代碼分割線 - 結束
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())