Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

pyqt highlight all occurrences of selected word qt

from PyQt4 import QtGui
from PyQt4 import QtCore

class MyHighlighter(QtGui.QTextEdit):
    def __init__(self, parent=None):
        super(MyHighlighter, self).__init__(parent)
        # Setup the text editor
        text = """In this text I want to highlight this word and only this word.
""" +
        """Any other word shouldn't be highlighted"""
        self.setText(text)
        cursor = self.textCursor()
        # Setup the desired format for matches
        format = QtGui.QTextCharFormat()
        format.setBackground(QtGui.QBrush(QtGui.QColor("red")))
        # Setup the regex engine
        pattern = "word"
        regex = QtCore.QRegExp(pattern)
        # Process the displayed document
        pos = 0
        index = regex.indexIn(self.toPlainText(), pos)
        while (index != -1):
            # Select the matched text and apply the desired format
            cursor.setPosition(index)
            cursor.movePosition(QtGui.QTextCursor.EndOfWord, 1)
            cursor.mergeCharFormat(format)
            # Move to the next match
            pos = index + regex.matchedLength()
            index = regex.indexIn(self.toPlainText(), pos)

if __name__ == "__main__":
    import sys
    a = QtGui.QApplication(sys.argv)
    t = MyHighlighter()
    t.show()
    sys.exit(a.exec_())
Comment

PREVIOUS NEXT
Code Example
Python :: nptel swayam 
Python :: kivy video recorder 
Python :: how to take matrix input in python 
Python :: google video processor python nmp 
Python :: explore data dataframe pandas 
Python :: edit packet in scapy 
Python :: colab erase recycle bin drive 
Python :: pyplot common labels 
Python :: jumpssh execute multiple commands 
Python :: pandas increment value on condition 
Python :: pandas crosstab function(counting) frequencies 
Python :: fibonacci series stackoverflow 
Python :: Remove outliers with median value and Capping 
Python :: python get all keys in dict having value in range 
Python :: python slicing string 
Python :: alignment to numpy array 
Python :: Overwrite text in python 
Python :: c vs python speed 
Python :: method 01 of making GUI with tkinter in python 
Python :: how to combine sets using update() Function 
Python :: choose what items on python 
Python :: python yellow 
Python :: how to strip characters in python 
Python :: python convert polygone to centroid 
Python :: aws ses service python example 
Python :: sum function in python 
Python :: python basic programs quadratic equation 
Python :: how to use kite python 
Python :: blue ray size 
Python :: tf.get_variable initializer 
ADD CONTENT
Topic
Content
Source link
Name
3+9 =