2017-01-19 3 views
2

코드는 QTextBrowser 텍스트 줄이 채워진 창을 만듭니다. "Long Line"과 일치하는 단어를 모두 선택하겠습니다. 어떻게 작성합니까?QTextBrowser에서 모든 항목을 선택하는 방법

enter image description here

from PyQt4 import QtCore, QtGui 
app = QtGui.QApplication([]) 

view = QtGui.QTextBrowser() 
for i in range(25): 
    view.append(10*('Long Line of text # %004d '%i)) 
view.setLineWrapMode(0) 

view.find('Long Line') 

view.show() 
app.exec_() 

답변

2

당신은 QTextEdit.setExtraSelections:

import sys 
from PyQt4.QtGui import (QApplication, QTextEdit, QTextBrowser, QTextCursor, 
         QTextCharFormat, QPalette) 

app = QApplication(sys.argv) 
view = QTextBrowser() 
for i in range(25): 
    view.append(10*('Long Line of text # %004d '%i)) 
view.setLineWrapMode(0) 
line_to_find = 'Long Line' 

# get default selection format from view's palette 
palette = view.palette() 
text_format = QTextCharFormat() 
text_format.setBackground(palette.brush(QPalette.Normal, QPalette.Highlight)) 
text_format.setForeground(palette.brush(QPalette.Normal, QPalette.HighlightedText)) 

# find all occurrences of the text 
doc = view.document() 
cur = QTextCursor() 
selections = [] 
while 1: 
    cur = doc.find(line_to_find, cur) 
    if cur.isNull(): 
     break 
    sel = QTextEdit.ExtraSelection() 
    sel.cursor = cur 
    sel.format = text_format 
    selections.append(sel) 
view.setExtraSelections(selections) 

view.show() 
app.exec_() 

을 사용할 수 있습니다 그리고 여기 결과입니다

the QTextBrowser on win7

또는 QSyntaxHighlighter 시도는.

관련 문제