PyQt5Day17--輸入控件QTextEdit純文本輸入


1、QTextEdit簡介

  是一個高級的查看器/編輯器,支持使用HTML樣式標簽的高文本格式;

  可以處理大型文檔並快速響應用戶輸入;

  適用於段落和字符;

  文本編輯可以加載純文本和高文本文件。

2、功能作用——占位文本設置

(1)框架

(2)功能操作及展示

1 # 占位文本提示
2 def 占用文本的提示(self):
3     self.te.setPlaceholderText('請輸入你的個人簡介')
4     print(self.te.placeholderText())

3、功能作用——內容設置

(1)框架

(2)功能操作及展示:普通文本、HTML、設置文本、追加文本、清空

 1 from PyQt5.Qt import *
 2 
 3 class Window(QWidget):
 4     def __init__(self):
 5         super().__init__()
 6         self.setWindowTitle("QTextEdit")
 7         self.resize(500, 500)
 8         self.setup_ui()
 9 
10     def setup_ui(self):
11         te = QTextEdit("xx",self)
12         self.te = te
13         te.move(50,50)
14         te.resize(300,300)
15         te.setStyleSheet('background-color:cyan')
16         # self.占用文本的提示()
17         self.文本內容的設置()
18 
19 
20         test_btn = QPushButton(self)
21         test_btn.move(10,10)
22         test_btn.setText("測試按鈕")
23         test_btn.pressed.connect(self.btn_test)
24 
25     def btn_test(self):
26         # 清空設置
27         self.te.setText("")
28         self.te.clear()
29 
30         print(self.te.document())
31         print(self.te.textCursor())
32 
33 
34     def 文本內容的設置(self):
35         # 設置普通文本內容
36         # self.te.setPlainText("<h1>xxx</h1>")
37         # self.te.insertPlainText("<h1>xxx</h1>") # 不覆蓋至之前的內容,在光標處插入
38         # print(self.te.toPlainText()) # 獲取內容
39 
40         # 富文本操作
41         # self.te.setHtml("<h1>xxx</h1>")
42         # self.te.insertHtml("<h2>牛逼<h2/>")
43         # print(self.te.toHtml())
44 
45         # 設置文本(自動判定)
46         # self.te.setText("<h1>ooo</h1>")
47 
48         # 追加文本(自動識別)
49         self.te.append("<h3>www<h3/>")  # 在文本的末尾追加
50 
51     # 占位文本提示
52     def 占用文本的提示(self):
53         self.te.setPlaceholderText('請輸入你的個人簡介')
54 
55 
56 if __name__ == '__main__':
57     import sys
58 
59     app=QApplication(sys.argv)
60 
61     window=Window()
62     window.show()
63     sys.exit(app.exec_())

(3)文本光標

  ① 添加內容

 1 # *********插入框架***********begin
 2 tc = self.te.textCursor()
 3 tff = QTextFrameFormat()
 4 tff.setBorder(10)
 5 tff.setBorderBrush(QColor(100,50,50))
 6 tff.setRightMargin(50)
 7 tc.insertFrame(tff)
 8 
 9 doc = self.te.document()
10 root_frame = doc.rootFrame() # 嵌入另一個框架
11 root_frame.setFrameFormat(tff)
12 
13 return None
14 # *********插入框架***********end
15 
16 # *********插入文本塊***********begin
17 tc = self.te.textCursor()
18 tbf = QTextBlockFormat()
19 tcf = QTextCharFormat()
20 tcf.setFontFamily('隸書')
21 tcf.setFontItalic(True) # 設置肢體傾斜
22 tcf.setFontPointSize(30)
23 tbf.setAlignment(Qt.AlignRight)
24 tbf.setRightMargin(100)
25 # tbf.setIndent(3)
26 tc.insertBlock(tbf,tcf) # 加入段落格式與文本格式
27 self.te.setFocus()
28 
29 return None
30 # *********插入文本塊***********end
31 
32 # *********插入表格***********begin
33 tc = self.te.textCursor()
34 # tc.insertTable(5,3)
35 ttf = QTextTableFormat()
36 ttf.setAlignment(Qt.AlignRight) # 右對齊
37 ttf.setCellPadding(6) # 內邊距
38 ttf.setCellSpacing(3) # 外邊距
39 ttf.setColumnWidthConstraints((QTextLength(QTextLength.PercentageLength,50),\
40                                QTextLength(QTextLength.PercentageLength,30),\
41                                QTextLength(QTextLength.PercentageLength,10))) # 寬度的約束
42 table = tc.insertTable(5,3,ttf)
43 table.appendColumns(2) # 增加2列
44 return None
45 # *********插入表格***********end
46 
47 # *********插入列表2***********begin
48 tc = self.te.textCursor()
49 tlf = QTextListFormat()
50 tlf.setIndent(3) # 縮進3個tab鍵
51 tlf.setNumberPrefix("<<<")  # 前綴
52 tlf.setNumberSuffix(">>>")  # 后綴
53 tlf.setStyle(QTextListFormat.ListDecimal)
54 tl = tc.createList(tlf)
55 print(tl)
56 return None
57 # *********插入列表2***********end
58 # *********插入列表1***********begin
59 tc = self.te.textCursor()
60 # tl = tc.insertList(QTextListFormat.ListCircle) # 列表的左邊為圓圈
61 # tl = tc.insertList(QTextListFormat.ListDecimal) # 列表的左邊為數字
62 tl = tc.createList(QTextListFormat.ListDecimal) # 創建(無論光標在哪兒,都將其變成一行)
63 print(tl)
64 return None
65 # *********插入列表1***********end
66 
67 # *********插入句子***********begin
68 tc = self.te.textCursor()
69 tdf = QTextDocumentFragment.fromHtml("<h1>xxx<h1/>")
70 tc.insertFragment(tdf)
71 return None
72 # *********插入句子***********end
73 
74 # *********插入圖片***********begin
75 tc = self.te.textCursor()
76 tif = QTextImageFormat()
77 tif.setName('xxx.png')
78 tif.setWidth(100)
79 tif.setHeight(100)
80 # tc.insertImage(tif) # 插入圖片,記住這一種
81 # tc.insertImage(tif,QTextFrameFormat.FloatRight) # 插入圖片靠右
82 tc.insertImage('close.png')  # 通過字符串插入圖片
83 return None
84 # *********插入圖片***********end
85 
86 # *********插入文本與超鏈接***********begin
87 tcf = QTextCharFormat()
88 tcf.setToolTip("fxb博客地址")
89 tcf.setFontFamily('隸書')
90 tcf.setFontPointSize(20)
91 tc = self.te.textCursor()
92 tc.insertText("itlike.com", tcf)  # 插入文本,並修改格式
93 
94 tc.insertHtml("<a href='http://52studyit.com'>hello</a>")  # 插入超鏈接
95 # *********插入文本與超鏈接***********end
添加內容

  ② 設置和合並格式 

 1 # *******************文本光標——設置和合並格式**********************開始
 2 from PyQt5.Qt import *
 3 
 4 class Window(QWidget):
 5     def __init__(self):
 6         super().__init__()
 7         self.setWindowTitle("QTextEdit")
 8         self.resize(500, 500)
 9         self.setup_ui()
10 
11     def setup_ui(self):
12         te = QTextEdit("xx",self)
13         self.te = te
14         te.move(50,50)
15         te.resize(300,300)
16         te.setStyleSheet('background-color:cyan')
17 
18         test_btn = QPushButton(self)
19         test_btn.move(10,10)
20         test_btn.setText("測試按鈕")
21         test_btn.pressed.connect(self.btn_test)
22 
23     def btn_test(self):
24         self.格式設置和合並()
25 
26     def 格式設置和合並(self):
27         # *********mergeBlockFormat***********begin
28         tc = self.te.textCursor()
29         tcf1 = QTextCharFormat()
30         tcf1.setFontFamily('幼圓')
31         tcf1.setFontPointSize(30)
32         tcf1.setFontOverline(True)  # 上划線
33         tcf1.setFontUnderline(True)  # 下划線
34         tc.setCharFormat(tcf1)
35 
36         tcf2 = QTextCharFormat()
37         tcf2.setFontStrikeOut(True)
38         # tc.setCharFormat(tcf2) # 單獨作用,兩個功能不能同時
39         tc.mergeCharFormat(tcf2) # 格式合並
40         return None
41         # *********mergeBlockFormat***********end
42         # *********setCharFormat***********begin
43         # 區別與setBlockCharFormat,選中文本而起作用
44         tc = self.te.textCursor()
45         tcf = QTextCharFormat()
46         tcf.setFontFamily('幼圓')
47         tcf.setFontPointSize(30)
48         tcf.setFontOverline(True)  # 上划線
49         tcf.setFontUnderline(True)  # 下划線
50         tc.setCharFormat(tcf)
51         return None
52         # *********setCharFormat***********end
53         # *********setBlockFormat***********begin
54         tc = self.te.textCursor()
55         tbf = QTextBlockFormat()
56         tbf.setAlignment(Qt.AlignCenter) # 居中對齊
57         tbf.setIndent(2) # 縮進
58         tc.setBlockFormat(tbf)
59         return None
60         # *********setBlockFormat***********end
61         # *********setBlockCharFormat***********begin
62         tc = self.te.textCursor()  # 文本光標對象
63         tcf = QTextCharFormat()
64         tcf.setFontFamily('幼圓')
65         tcf.setFontPointSize(30)
66         tcf.setFontOverline(True)  # 上划線
67         tcf.setFontUnderline(True)  # 下划線
68         tc.setBlockCharFormat(tcf)
69         # *********setBlockCharFormat***********end
70 
71 if __name__ == '__main__':
72     import sys
73 
74     app=QApplication(sys.argv)
75 
76     window=Window()
77     window.show()
78     sys.exit(app.exec_())
79 # *******************文本光標——設置和合並格式**********************結束
設置和合並格式

  ③ 獲取內容和格式相關

 1 # *******************文本光標——獲取內容和格式相關**********************開始
 2 from PyQt5.Qt import *
 3 
 4 class Window(QWidget):
 5     def __init__(self):
 6         super().__init__()
 7         self.setWindowTitle("QTextEdit")
 8         self.resize(500, 500)
 9         self.setup_ui()
10 
11     def setup_ui(self):
12         te = QTextEdit("xx",self)
13         self.te = te
14         te.move(50,50)
15         te.resize(300,300)
16         te.setStyleSheet('background-color:cyan')
17 
18         test_btn = QPushButton(self)
19         test_btn.move(10,10)
20         test_btn.setText("測試按鈕")
21         test_btn.pressed.connect(self.btn_test)
22 
23         # 插入列表
24         tlf = QTextListFormat()
25         tlf.setIndent(3)
26         tlf.setNumberPrefix('<<')
27         tlf.setNumberSuffix('>>')
28         tlf.setStyle(QTextListFormat.ListDecimal)
29         te.textCursor().createList(tlf)
30 
31     def btn_test(self):
32         self.獲取內容和格式相關()
33 
34     def 獲取內容和格式相關(self):
35         tc = self.te.textCursor()
36         print(tc.block().text()) # 獲取光標所在的文本
37         print(tc.blockNumber()) # 獲取光標所在的文本塊編號(段落編號)
38         print(tc.currentList().count()) # 獲取當前所在的文本列表
39 
40 if __name__ == '__main__':
41     import sys
42 
43     app=QApplication(sys.argv)
44 
45     window=Window()
46     window.show()
47     sys.exit(app.exec_())
48 # *******************文本光標——獲取內容和格式相關**********************結束
獲取內容和格式相關

  ④ 文本選中和清空

 1 # *******************文本光標——文本選中和清空**********************開始
 2 from PyQt5.Qt import *
 3 
 4 class Window(QWidget):
 5     def __init__(self):
 6         super().__init__()
 7         self.setWindowTitle("QTextEdit")
 8         self.resize(500, 500)
 9         self.setup_ui()
10 
11     def setup_ui(self):
12         te = QTextEdit("xx",self)
13         self.te = te
14         te.move(50,50)
15         te.resize(300,300)
16         te.setStyleSheet('background-color:cyan')
17 
18         test_btn = QPushButton(self)
19         test_btn.move(10,10)
20         test_btn.setText("測試按鈕")
21         test_btn.pressed.connect(self.btn_test)
22 
23         # 插入表格
24         te.textCursor().insertTable(5,3)
25 
26     def btn_test(self):
27         self.文本選中和清空()
28 
29     def 文本選中和清空(self):
30         # *********選中的位置獲取***********begin
31         tc = self.te.textCursor()
32         # print(tc.selectionStart()) # 光標開始位置
33         # print(tc.selectionEnd()) # 光標結束位置
34         # tc.clearSelection() # 取消選中
35         # self.te.setTextCursor(tc)
36         # print(tc.hasSelection()) # 查看是否選中內容
37         tc.removeSelectedText() # 移除選中內容
38         self.te.setFocus()
39         return None
40         # *********選中的位置獲取***********end
41         
42         # *********選中內容獲取***********begin
43         tc = self.te.textCursor()
44         # print(tc.selectedText())
45         # print(tc.selection().toPlainText())
46         print(tc.selectedTableCells()) # 獲取表格選中的范圍
47         return None
48         # *********選中內容獲取***********end
49 
50         # *********選中+補充***********begin
51         tc = self.te.textCursor()
52         # tc.setPosition(6) # 設置光標位置
53         # tc.movePosition(QTextCursor.StartOfLine,QTextCursor.MoveAnchor,1) # 光標移至段落開頭
54         # tc.movePosition(QTextCursor.StartOfLine,QTextCursor.KeepAnchor,1) # 選中到當前行首之間的內容
55         # tc.movePosition(QTextCursor.Up,QTextCursor.KeepAnchor,1) # 選中到當前行光標與第一行段落同一位置光標之間的內容
56         # tc.select(QTextCursor.BlockUnderCursor) # 選中當前光標下的文本塊
57         tc.select(QTextCursor.WordUnderCursor)  # 選中當前光標下的單詞
58         self.te.setTextCursor(tc)  # 反向設置回去
59         self.te.setFocus()
60         # *********選中+補充***********end
61 
62 if __name__ == '__main__':
63     import sys
64 
65     app=QApplication(sys.argv)
66 
67     window=Window()
68     window.show()
69     sys.exit(app.exec_())
70 # *******************文本光標——文本選中和清空**********************結束
文本選中和清空

  ⑤ 刪除內容

1 # *********刪除文本字符***********begin
2 tc = self.te.textCursor()
3 # tc.deleteChar() # 刪除光標右側的內容(如果沒有選中內容)
4 tc.deletePreviousChar()
5 self.te.setFocus()
6 return None
7 # *********刪除文本字符***********end
刪除文本字符

  ⑥ 位置相關

 1 # *******************文本光標——位置相關**********************開始
 2 from PyQt5.Qt import *
 3 
 4 class Window(QWidget):
 5     def __init__(self):
 6         super().__init__()
 7         self.setWindowTitle("QTextEdit")
 8         self.resize(500, 500)
 9         self.setup_ui()
10 
11     def setup_ui(self):
12         te = QTextEdit("xx",self)
13         self.te = te
14         te.move(50,50)
15         te.resize(300,300)
16         te.setStyleSheet('background-color:cyan')
17 
18         test_btn = QPushButton(self)
19         test_btn.move(10,10)
20         test_btn.setText("測試按鈕")
21         test_btn.pressed.connect(self.btn_test)
22 
23         # 插入表格
24         te.textCursor().insertTable(5,3)
25 
26     def btn_test(self):
27         self.位置相關()
28 
29     def 位置相關(self):
30         tc = self.te.textCursor()
31         print('是否在段落的結尾',tc.atBlockEnd())
32         print('是否在段落的開始',tc.atBlockStart())
33         print('是否在文檔的結尾',tc.atEnd())
34         print('是否在文檔的開始',tc.atStart())
35         print('在第幾列',tc.columnNumber())
36         print('光標位置',tc.position())
37         print('在文本塊中的位置',tc.positionInBlock())
38 
39 if __name__ == '__main__':
40     import sys
41     app=QApplication(sys.argv)
42     window=Window()
43     window.show()
44     sys.exit(app.exec_())
45 # *******************文本光標——位置相關**********************結束
位置相關

  ⑦ 開始和結束編輯標識

 1 # *******************文本光標——開始和結束編輯標識***********************開始
 2 from PyQt5.Qt import *
 3 
 4 class Window(QWidget):
 5     def __init__(self):
 6         super().__init__()
 7         self.setWindowTitle("QTextEdit")
 8         self.resize(500, 500)
 9         self.setup_ui()
10 
11     def setup_ui(self):
12         te = QTextEdit("xx",self)
13         self.te = te
14         te.move(50,50)
15         te.resize(300,300)
16         te.setStyleSheet('background-color:cyan')
17 
18         test_btn = QPushButton(self)
19         test_btn.move(10,10)
20         test_btn.setText("測試按鈕")
21         test_btn.pressed.connect(self.btn_test)
22 
23         # 插入表格
24         te.textCursor().insertTable(5,3)
25 
26     def btn_test(self):
27         self.開始和結束編輯標識()
28 
29     def 開始和結束編輯標識(self):
30         tc = self.te.textCursor()
31 
32         tc.beginEditBlock() # 以下操作整合為一個操作
33         tc.insertText('123')
34         tc.insertBlock()
35         tc.insertText('456')
36         tc.insertBlock()
37         tc.endEditBlock() # 以上操作整合為一個操作
38 
39         tc.insertText('789')
40         tc.insertBlock()
41 
42 if __name__ == '__main__':
43     import sys
44     app=QApplication(sys.argv)
45     window=Window()
46     window.show()
47     sys.exit(app.exec_())
48 # *******************文本光標——開始和結束編輯標識***********************結束
開始和結束編輯標識

4、功能作用——自動格式化

(1)框架

(2)功能操作及展示

 1 # *******************自動格式化**********************開始
 2 from PyQt5.Qt import *
 3 
 4 class Window(QWidget):
 5     def __init__(self):
 6         super().__init__()
 7         self.setWindowTitle("QTextEdit")
 8         self.resize(500, 500)
 9         self.setup_ui()
10 
11     def setup_ui(self):
12         te = QTextEdit("xx",self)
13         self.te = te
14         te.move(50,50)
15         te.resize(300,300)
16         te.setStyleSheet('background-color:cyan')
17 
18         test_btn = QPushButton(self)
19         test_btn.move(10,10)
20         test_btn.setText("測試按鈕")
21         test_btn.pressed.connect(self.btn_test)
22 
23         # 插入表格
24         te.textCursor().insertTable(5,3)
25 
26     def btn_test(self):
27         self.自動格式化()
28 
29     def 自動格式化(self):
30         self.te.setAutoFormatting(QTextEdit.AutoBulletList) # 自動創建項目符號列表
31 
32 if __name__ == '__main__':
33     import sys
34     app=QApplication(sys.argv)
35     window=Window()
36     window.show()
37     sys.exit(app.exec_())
38 # *******************自動格式化**********************結束
自動格式化

5、功能作用——轉換行模式

(1)框架

(2)功能操作及展示

 1 # *******************轉換行模式**********************開始
 2 from PyQt5.Qt import *
 3 
 4 class Window(QWidget):
 5     def __init__(self):
 6         super().__init__()
 7         self.setWindowTitle("QTextEdit")
 8         self.resize(500, 500)
 9         self.setup_ui()
10 
11     def setup_ui(self):
12         te = QTextEdit("xx",self)
13         self.te = te
14         te.move(50,50)
15         te.resize(300,300)
16         te.setStyleSheet('background-color:cyan')
17 
18         test_btn = QPushButton(self)
19         test_btn.move(10,10)
20         test_btn.setText("測試按鈕")
21         test_btn.pressed.connect(self.btn_test)
22 
23         # 插入表格
24         te.textCursor().insertTable(5,3)
25 
26     def btn_test(self):
27         self.轉換行模式()
28 
29     def 轉換行模式(self):
30         # self.te.setLineWrapMode(QTextEdit.NoWrap) # 沒有軟換行模式(有水平滾動條)
31         self.te.setLineWrapMode(QTextEdit.FixedPixelWidth) # 設置固定的像素寬度
32         self.te.setLineWrapColumnOrWidth(100)  # 這里的100與上面的FixePixelWidth有關
33 
34         self.te.setWordWrapMode(QTextOption.WrapMode)  # 保持單詞的完整性
35 
36 if __name__ == '__main__':
37     import sys
38     app=QApplication(sys.argv)
39     window=Window()
40     window.show()
41     sys.exit(app.exec_())
42 # *******************轉換行模式**********************結束
轉換行模式

6、功能作用——覆蓋模式

(1)框架

(2)功能操作及展示

1 def 覆蓋模式(self):
2     self.te.setOverwriteMode(True) # 覆蓋輸入
3     print(self.te.overwriteMode()) # 查看覆蓋輸入狀態

7、功能作用——光標設置

(1)框架

(2)功能操作及展示

1 def 光標設置(self):
2     if self.te.overwriteMode():
3         self.te.setOverwriteMode(False)
4         self.te.setCursorWidth(1)  # 設置光標寬度
5     else:
6         self.te.setOverwriteMode(True)
7         self.te.setCursorWidth(10)

8、功能作用——對齊方式

(1)框架

(2)功能操作及展示

1 self.te.setAlignment(Qt.AlignCenter)

9、功能作用——字體格式

(1)框架

(2)功能操作及展示

 1 def 字體設置(self):
 2     # QFontDialog.getFont() # 小技巧
 3     # self.te.setFontFamily("幼圓") # 設置字體
 4     # self.te.setFontWeight(QFont.Black) # 設置字體的粗細
 5     # self.te.setFontItalic(True) # 設置字體的傾斜
 6     # self.te.setFontPointSize(25) # 設置字體的大小
 7     # self.te.setFontUnderline(True) #設置下划線
 8 
 9     font = QFont()
10     font.setStrikeOut(True) # 刪除(中間一根線)
11     self.te.setCurrentFont(font)

10、功能操作——顏色設置

(1)框架

(2)功能操作及展示

1 def 顏色設置(self):
2     self.te.setTextBackgroundColor(QColor(200,10,10))  # 設置文本背景顏色
3     self.te.setTextColor(QColor(10,200,10)) # 設置文本的顏色

11、功能作用——當前的字符格式

(1)框架

(2)功能操作及展示

 1 def 當前字符格式(self):
 2     tcf = QTextCharFormat()
 3     tcf.setFontFamily("宋體")
 4     tcf.setFontPointSize(25)
 5     tcf.setFontCapitalization(QFont.Capitalize) # 首字母大寫
 6     tcf.setForeground(QColor(100,200,150)) # 設置顏色
 7     self.te.setCurrentCharFormat(tcf)
 8 
 9     tcf2 = QTextCharFormat()
10     tcf2.setFontOverline(True)
11     # self.te.setCurrentCharFormat(tcf2) # 覆蓋掉tcf
12     self.te.mergeCurrentCharFormat(tcf2)  # tcf和tcf2合並

 12、功能作用——常用編輯操作

(1)框架

(2)功能操作及展示

 1 def 常用編輯操作(self):
 2     # self.te.copy()
 3     # self.te.paste()
 4     # print(self.te.canPaste())
 5     # self.te.selectAll() # 全選
 6 
 7     # print(self.te.find("xx"))  # 默認從左向右查找,並返回結果
 8     # print(self.te.find("xx",QTextDocument.FindBackward))  # 從右向左查找,並返回結果
 9     # print(self.te.find("xx",QTextDocument.FindBackward | QTextDocument.FindCaseSensitively))  # 從右向左查找,區分大小寫,並返回結果
10     print(self.te.find("xx",QTextDocument.FindWholeWords))  # 查找完整的單詞,並返回結果
11     self.te.setFocus()

13、功能作用——滾動+只讀設置

(1)框架

(2)功能操作及展示

1 te.insertHtml("xxx"*300+"<a name='lk' href='#itlike'>哈哈</a>"+"aaa"*200)
2 
3 def 滾動到錨點(self):
4     self.te.scrollToAnchor("lk")
1 def 只讀設置(self):
2     self.te.setReadOnly(True)
3     self.te.insertPlainText('itlike') # 不能限制通過代碼插入內容
4     print(self.te.isReadOnly()) # 查看是否只讀

14、功能作用——tab控制

(1)框架

(2)功能操作及展示

1 def tab控制(self):
2     # self.te.setTabChangesFocus(True) # tab鍵改變焦點
3     print(self.te.tabStopDistance()) # 查看制表符的距離
4     self.te.setTabStopDistance(100) # 設置制表符的距離
5     print(self.te.tabStopDistance())  # 查看制表符的距離

15、功能作用——錨點獲取

(1)框架

(2)功能操作及展示

 1 # *******************錨點獲取**********************開始
 2 from PyQt5.Qt import *
 3 
 4 class MyTextEdit(QTextEdit):
 5     def mousePressEvent(self, me):
 6         print(me.pos())
 7         link_str = self.anchorAt(me.pos())
 8         if len(link_str) > 0:
 9             QDesktopServices.openUrl(QUrl(link_str))
10 
11 class Window(QWidget):
12     def __init__(self):
13         super().__init__()
14         self.setWindowTitle("QTextEdit")
15         self.resize(500, 500)
16         self.setup_ui()
17 
18     def setup_ui(self):
19         te = MyTextEdit("xx",self)
20         self.te = te
21         te.move(50,50)
22         te.resize(300,300)
23         te.setStyleSheet('background-color:cyan')
24 
25         test_btn = QPushButton(self)
26         test_btn.move(10,10)
27         test_btn.setText("測試按鈕")
28         test_btn.pressed.connect(self.btn_test)
29 
30         # 插入表格
31         te.textCursor().insertTable(5,3)
32 
33         te.insertHtml("xxx" * 300 + "<a href='https://www.cnblogs.com/fengxb1213/'>博客</a>" + "aaa" * 200)
34 
35     def btn_test(self):
36         self.錨點獲取()
37 
38     def 錨點獲取(self):
39         # 打開超鏈接,看上面的重寫QTextDdit里面的mousePressEvent
40         pass
41 
42 if __name__ == '__main__':
43     import sys
44     app=QApplication(sys.argv)
45     window=Window()
46     window.show()
47     sys.exit(app.exec_())
48 # *******************錨點獲取**********************結束

16、信號

(1)框架

(2)功能操作及展示

 1 # *******************信號**********************開始
 2 from PyQt5.Qt import *
 3 
 4 class Window(QWidget):
 5     def __init__(self):
 6         super().__init__()
 7         self.setWindowTitle("QTextEdit")
 8         self.resize(500, 500)
 9         self.setup_ui()
10 
11     def text_change(self):
12         print("文本內容發生了改變")
13 
14     def selection_change(self):
15         print("文本選中的內容發生了改變")
16 
17     def copy_a(self,yes):
18         print("復制是否可用",yes)
19 
20     def setup_ui(self):
21         te = QTextEdit("xx",self)
22         self.te = te
23         te.move(50,50)
24         te.resize(300,300)
25         te.setStyleSheet('background-color:cyan')
26 
27         # te.textChanged.connect(self.text_change)
28         # te.selectionChanged.connect(self.selection_change)
29         te.copyAvailable.connect(self.copy_a)
30 
31         test_btn = QPushButton(self)
32         test_btn.move(10,10)
33         test_btn.setText("測試按鈕")
34         test_btn.pressed.connect(self.btn_test)
35 
36         # 插入表格
37         te.textCursor().insertTable(5,3)
38 
39         te.insertHtml("xxx" * 300 + "<a href='https://www.cnblogs.com/fengxb1213/'>博客</a>" + "aaa" * 200)
40 
41     def btn_test(self):
42         pass
43 
44 if __name__ == '__main__':
45     import sys
46     app=QApplication(sys.argv)
47     window=Window()
48     window.show()
49     sys.exit(app.exec_())
50 # *******************信號**********************結束

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM