[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
(http://forum.qtfr.org/viewtopic.php?id=4447):
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.te=QtGui.QTextEdit()
self.setCentralWidget(self.te)
self.highlighter = generic_highlighter.genericHighlighter(self.te)
self.highlighter.Cpp()
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
cur.setBlockCharFormat(aHighlightingRule.getFormat())
blockHighlighted = True
if not blockHighlighted :
# if the current block does not match any highlightring
# rules, format it with the default settings.
cur.setBlockCharFormat(QtGui.QTextCharFormat())
# go to the next block
cur.movePosition(QtGui.QTextCursor.NextBlock)
# get the cursor position near the bottom left corner of the
current viewport.
endCursor =
self.te.cursorForPosition(QtCore.QPoint(0,self.te.viewport().height()))
if cur.block().position() > endCursor.block().position() :
currentBlockIsVisible = False
app=QtGui.QApplication(sys.argv)
mw=MainWindow()
mw.show()
app.exec_()
>-----------------------------------
Any idea ?
Thanks.
More information about the PyQt
mailing list