高亮文本
This commit is contained in:
parent
cc7a62bf3c
commit
6fdceac9a4
4 changed files with 65 additions and 1 deletions
|
@ -14,6 +14,7 @@
|
|||
[腾讯视频热播列表](腾讯视频热播列表/)
|
||||
|
||||
[ QWebView 与 python 通过js交互 特效 - 已失效 未更新](https://github.com/892768447/PhotoEffects)
|
||||
|
||||
### II、功能型
|
||||
|
||||
#### 2.1 常用例子
|
||||
|
@ -40,6 +41,7 @@
|
|||
- [ 右下角弹出框](右下角弹出框/)
|
||||
- [ 消息对话框倒计时关闭](消息对话框倒计时关闭/)
|
||||
|
||||
- [QTextEdit 文本高亮](文本高亮/)
|
||||
- win 32
|
||||
- [ 嵌入外部窗口](嵌入外部窗口/)
|
||||
- [ 探测窗口和 放大镜](探测窗口和放大截图/)
|
||||
|
@ -51,6 +53,7 @@
|
|||
|
||||
- [ QListView 加按钮和 排序](QListView/)
|
||||
- [ Json生成QTreeWidget](Json生成QTreeWidget/)
|
||||
- [ treeWidget 节点可拖拽](https://github.com/AshotS/glowing-disco)
|
||||
- [ QSqlTableModel + QTableView 数据库查询显示表格](数据库查询显示表格/)
|
||||
|
||||
|
||||
|
|
4
文本高亮/README.md
Normal file
4
文本高亮/README.md
Normal file
|
@ -0,0 +1,4 @@
|
|||
# QTextEdit mergeCurrentCharFormat函数
|
||||
|
||||
|
||||
![1](ScreenShot/1.gif)
|
BIN
文本高亮/ScreenShot/1.gif
Normal file
BIN
文本高亮/ScreenShot/1.gif
Normal file
Binary file not shown.
After Width: | Height: | Size: 471 KiB |
57
文本高亮/简单查找文字并高亮.py
Normal file
57
文本高亮/简单查找文字并高亮.py
Normal file
|
@ -0,0 +1,57 @@
|
|||
import sys
|
||||
from PyQt5.QtGui import QTextCharFormat,QTextDocument, QTextCursor
|
||||
from PyQt5.QtWidgets import (QApplication, QMainWindow, QTextEdit,
|
||||
QToolBar, QLineEdit, QPushButton, QColorDialog, QHBoxLayout, QWidget)
|
||||
|
||||
class TextEdit(QMainWindow):
|
||||
def __init__(self, parent=None):
|
||||
super(TextEdit, self).__init__(parent)
|
||||
self.textEdit = QTextEdit(self)
|
||||
self.setCentralWidget(self.textEdit)
|
||||
|
||||
widget = QWidget(self)
|
||||
vb = QHBoxLayout(widget)
|
||||
vb.setContentsMargins(0, 0, 0, 0)
|
||||
self.findText = QLineEdit(self)
|
||||
self.findText.setText('self')
|
||||
findBtn = QPushButton('高亮',self)
|
||||
findBtn.clicked.connect(self.highlight)
|
||||
vb.addWidget(self.findText)
|
||||
vb.addWidget(findBtn)
|
||||
|
||||
tb = QToolBar(self)
|
||||
tb.addWidget(widget)
|
||||
|
||||
def setText(self,text):
|
||||
self.textEdit.setPlainText(text)
|
||||
|
||||
def mergeFormatOnWordOrSelection(self, format):
|
||||
cursor = self.textEdit.textCursor()
|
||||
if not cursor.hasSelection():
|
||||
cursor.select(QTextCursor.WordUnderCursor)
|
||||
cursor.mergeCharFormat(format)
|
||||
self.textEdit.mergeCurrentCharFormat(format)
|
||||
|
||||
def highlight(self):
|
||||
text = self.findText.text()#输入框中的文字
|
||||
if not text:
|
||||
return
|
||||
col = QColorDialog.getColor(self.textEdit.textColor(), self)
|
||||
if not col.isValid():
|
||||
return
|
||||
fmt = QTextCharFormat()
|
||||
fmt.setForeground(col)
|
||||
#先把光标移动到开头
|
||||
self.textEdit.moveCursor(QTextCursor.Start)
|
||||
while self.textEdit.find(text,QTextDocument.FindWholeWords):#查找所有文字
|
||||
self.mergeFormatOnWordOrSelection(fmt)
|
||||
|
||||
if __name__ == '__main__':
|
||||
app = QApplication(sys.argv)
|
||||
|
||||
textEdit = TextEdit()
|
||||
textEdit.resize(800, 600)
|
||||
textEdit.show()
|
||||
textEdit.setText(open(sys.argv[0],'rb').read().decode())
|
||||
|
||||
sys.exit(app.exec_())
|
Loading…
Reference in a new issue