Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 11-13-2009, 01:09 PM   #1
LQ Newbie
Registered: Mar 2007
Location: France
Distribution: Ubuntu 9.04
Posts: 15

Rep: Reputation: 0
Porting Qt highlighting example code to PyQt4

[Edit]See post #2 for a simplified version of the problem (I think)[/Edit]

Hello everyone,

I'm trying to port the Qt syntax highlighting example to PyQt4 and I am having a few problems. The code seems to work but nothing is highlighted and sometimes (when i try to modify the beginning of a line or the end of a line that should be highlighted), I get stucked in a infinite loop.

Here is the example code :

And here is mine :
# -*- coding: utf-8 -*-

from PyQt4 import QtCore, QtGui

def gimme_my_mappings():
    a = QtGui.QTextCharFormat()

    functionFormat = QtGui.QTextCharFormat()

    mappings = {}
    mappings["%[^\n]*"] = a
    mappings["\\b[a-z0-9_]+\\(.*\\)"] = functionFormat
    return mappings

def highlight(doc, position, removed, added):
    block = doc.findBlock(position)
    print block, doc, position, removed, added

    if not block.isValid():
        print "not valid"
        return None
    if added>removed:
        endBlock = doc.findBlock(position+added)
        endBlock = block
    while (block.isValid() and not (endBlock < block)):
        block =

def highlightBlock(block):
    mappings = gimme_my_mappings()
    layout = block.layout()
    text = block.text()
    print text
    overrides = []
    for k in mappings.keys():
        expression = QtCore.QRegExp(k)
        i = text.indexOf(expression)
        while i>=0:
            #here is the infinite loop !
            range = QtGui.QTextLayout.FormatRange()
            range.start = i
            range.length = expression.matchedLength()
            range.format = mappings[k]
            i = text.indexOf(expression, i + expression.matchedLength())
    block.document().markContentsDirty(block.position(), block.length())
These functions are then called from my main application that contains a QTextEdit named textEdit with :
and :
def highlight1(self, position, removed, added):
    highlight(self.ui_prefs.textEdit.document(), position, removed, added)

I have re-read the code a thousand times and I can't find what I missed. If anyone has an idea, I'd be happy to hear it !!

Thanks !

Last edited by Snouffelaire; 11-20-2009 at 12:03 PM. Reason: problem has evolved
Old 11-14-2009, 01:14 AM   #2
LQ Newbie
Registered: Mar 2007
Location: France
Distribution: Ubuntu 9.04
Posts: 15

Original Poster
Rep: Reputation: 0
Things have changed a little but the problem is the same...

I found out that there was a QSyntaxHighlighter class that sould handle syntax highlighting problems :

I tried making a new app so I would be sure I didn't miss anything and I'm still stuck in an infinite loop... I did copy/paste the example code given on trolltech's site (I mean, I transformed it to Python). I made a Dialog with Designer and pyuic4 it (it is named :
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'gui.ui'
# Created: Sat Nov 14 07:46:19 2009
#      by: PyQt4 UI code generator 4.4.4
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.resize(400, 300)
        self.gridLayout = QtGui.QGridLayout(Dialog)
        self.textEdit = QtGui.QTextEdit(Dialog)
        self.gridLayout.addWidget(self.textEdit, 0, 0, 1, 1)
        self.buttonBox = QtGui.QDialogButtonBox(Dialog)
        self.gridLayout.addWidget(self.buttonBox, 1, 0, 1, 1)

        QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("accepted()"), Dialog.accept)
        QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("rejected()"), Dialog.reject)

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Dialog", None, QtGui.QApplication.UnicodeUTF8))
and here is the main application :
# -*- coding: utf-8 -*-

from PyQt4 import QtCore, QtGui
from gui import *

class MyApp(Ui_Dialog):
    def setupUi2(self, Dialog):
        Ui_Dialog.setupUi(self, Dialog)
        self.highlighter = MyHighlighter(self.textEdit)

class MyHighlighter(QtGui.QSyntaxHighlighter):
    def __init__(self, edit):
    def highlightBlock(self, text):        
        myClassFormat = QtGui.QTextCharFormat()
        pattern = "\\b[A-Z_]+\\b"
        expression = QtCore.QRegExp(pattern)
        index = text.indexOf(expression);
        while (index >= 0):
            length = expression.matchedLength()
            self.setFormat(index, length, myClassFormat)
            index = text.indexOf(expression, index + length)

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    Dialog = QtGui.QDialog()
    ui = MyApp()
Any help would be greatly appreciated

Last edited by Snouffelaire; 11-14-2009 at 01:17 AM.
Old 11-17-2009, 06:56 AM   #3
LQ Newbie
Registered: Mar 2007
Location: France
Distribution: Ubuntu 9.04
Posts: 15

Original Poster
Rep: Reputation: 0
Ok, I finally found an answer... I forgot this little line :
that should be added in the setupUI2 function.

Hope this will help someone in the future !
Old 11-17-2009, 12:28 PM   #4
LQ Newbie
Registered: Mar 2007
Location: France
Distribution: Ubuntu 9.04
Posts: 15

Original Poster
Rep: Reputation: 0
I actually found out that this was never the problem... I just need to update PyQt4 as I still get stuck in the loop with version 4.4.4. and not with the version I have at work.

Edit : the version I have at work is 4.3.3 so there must be a bug in version 4.4.4. I'm perplex.

Last edited by Snouffelaire; 11-18-2009 at 03:58 AM.
Old 11-20-2009, 12:02 PM   #5
LQ Newbie
Registered: Mar 2007
Location: France
Distribution: Ubuntu 9.04
Posts: 15

Original Poster
Rep: Reputation: 0
I had an answer to my question on the python usenet group.

If someone has the same problem, here is an answer :


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
porting code to gcc ..template code problems aayudh Programming 1 01-17-2009 03:40 PM
Porting Code lucky6969b Programming 5 12-14-2005 10:11 PM
porting win code quarry_06 Programming 3 11-24-2004 10:41 PM
Porting C++ code from XP to Linux Bondfire Programming 7 08-12-2004 03:42 PM
Porting c++ code to linux Moons Programming 3 06-03-2004 01:24 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:31 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration