[PyQt] Translating a line from a C++ source

kib2 kib2 at free.fr
Tue Oct 30 16:28:23 GMT 2007

David Boddie a écrit :
> Over multiple lines:
>   print "void MyTextEdit::paintEvent(QPaintEvent* event) from position:",
>   print startPosition.block().position(), "to",
>   print endPosition.block().position() + endPosition.block().length()
> Hope this helps,
Thanks David,

that works fine.
Now, I'm trying to achieve something that I found in a C++ forum 
To set my current QTextEdit's highlighter to highlight only the 
QTextEdit's viewport because it is very slow with large docs.

I've tried to translate it, but it seems to enter an infinite loop.
Maybe I missed something in translating this to Python, and the original 
C++ for loop is a particular one.

import sys
from PyQt4 import QtGui, QtCore
import generic_highlighter
class MainWindow(QtGui.QMainWindow):
    def __init__(self, *args):
        QtGui.QMainWindow.__init__(self, *args)
        self.highlighter = generic_highlighter.genericHighlighter(self.te)
        self.connect( self.te.verticalScrollBar(), QtCore.SIGNAL( 
"sliderMoved(int)" ), self.quickRehighlight() )
    def quickRehighlight(self):
        # save the current cursor position in the document
        cursor_saved = self.te.textCursor()
        # get the cursor position near the top
        # left corner of the current viewport.
        cur = self.te.cursorForPosition(QtCore.QPoint(0,0))
        currentBlockIsVisible = True
        while currentBlockIsVisible :
            blockHighlighted = False
            # find if the current block should be highlighted or not
            longrules = len(self.highlighter.rules)
            while not blockHighlighted :
                for i in range(longrules) :
                    aHighlightingRule = self.highlighter.rules[i]
                    #pattern = aHighlightingRule.pattern
                    regexp = aHighlightingRule.pattern
                    if cur.block().text().indexOf(regexp) >= 0 :
                        # reformating will force the textEdit to send 
the signal textChanged
                        blockHighlighted = True

            if not blockHighlighted :
                # if the current block does not match any highlightring
                # rules, format it with the default settings.

            # go to the next block
            # get the cursor position near the bottom left corner of the 
current viewport.
            endCursor = 

            if cur.block().position() > endCursor.block().position() :
                currentBlockIsVisible = False


Any idea ?


More information about the PyQt mailing list