Advertisement
Guest User

Untitled

a guest
Jan 4th, 2011
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.53 KB | None | 0 0
  1. import sys
  2. import re
  3.  
  4. from PyQt4.QtGui import *
  5. from PyQt4.QtCore import *
  6.  
  7.  
  8. class Window(QWidget):
  9.  
  10.     def __init__(self):
  11.         QWidget.__init__(self)
  12.         self.patIsWord = re.compile('\w+')
  13.  
  14.         vbox = QVBoxLayout(self)
  15.         self.text = QPlainTextEdit()
  16.         self.text.setPlainText('Esto es una prueba\nprueba de ejemplo\nnada mas que decir')
  17.         vbox.addWidget(self.text)
  18.  
  19.         self.connect(self.text, SIGNAL("cursorPositionChanged()"), self.highlight_current_word)
  20.  
  21.     def text_under_cursor(self):
  22.         tc = self.text.textCursor()
  23.         tc.select(QTextCursor.WordUnderCursor)
  24.         return tc.selectedText()
  25.  
  26.     def highlight_current_word(self):
  27.         self.extraSelections = []
  28.  
  29.         word = self.text_under_cursor()
  30.         if self.patIsWord.match(word):
  31.             lineColor = QColor(Qt.red)
  32.             lineColor.setAlpha(100)
  33.             cursor = self.text.document().find(word)
  34.             block = self.text.firstVisibleBlock()
  35.             while block.isValid():
  36.                 selection = QTextEdit.ExtraSelection()
  37.                 selection.format.setBackground(lineColor)
  38.                 selection.format.setToolTip(word)
  39.                 selection.cursor = cursor
  40.                 self.extraSelections.append(selection)
  41.                 cursor = self.text.document().find(word, cursor.position())
  42.                 block = block.next()
  43.         self.text.setExtraSelections(self.extraSelections)
  44.  
  45.  
  46. app = QApplication(sys.argv)
  47. w = Window()
  48. w.show()
  49.  
  50. sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement