LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

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

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 :
http://qt.nokia.com/doc/4.0/richtext...ghlighter.html

And here is mine :
Code:
#!/usr/bin/python
# -*- coding: utf-8 -*-

from PyQt4 import QtCore, QtGui


def gimme_my_mappings():
    a = QtGui.QTextCharFormat()
    a.setFontWeight(QtGui.QFont.Bold)
    a.setForeground(QtCore.Qt.blue)

    functionFormat = QtGui.QTextCharFormat()
    functionFormat.setFontItalic(True)
    functionFormat.setForeground(QtCore.Qt.red)

    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)
    else:
        endBlock = block
    
    while (block.isValid() and not (endBlock < block)):
        highlightBlock(block)
        block = block.next()


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]
            overrides.append(range)
            i = text.indexOf(expression, i + expression.matchedLength())
        
    layout.setAdditionalFormats(overrides)
    block.document().markContentsDirty(block.position(), block.length())
These functions are then called from my main application that contains a QTextEdit named textEdit with :
Code:
QtCore.QObject.connect(self.ui_prefs.textEdit.document(),\
     QtCore.SIGNAL("contentsChange(int,int,int)"),self.highlight1)
and :
Code:
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
Snouffelaire
LQ Newbie
 
Registered: Mar 2007
Location: France
Distribution: Ubuntu 9.04
Posts: 13

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 :
http://www.riverbankcomputing.co.uk/...ghlighter.html

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 gui.py) :
Code:
# -*- 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.setObjectName("Dialog")
        Dialog.resize(400, 300)
        self.gridLayout = QtGui.QGridLayout(Dialog)
        self.gridLayout.setObjectName("gridLayout")
        self.textEdit = QtGui.QTextEdit(Dialog)
        self.textEdit.setObjectName("textEdit")
        self.gridLayout.addWidget(self.textEdit, 0, 0, 1, 1)
        self.buttonBox = QtGui.QDialogButtonBox(Dialog)
        self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
        self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
        self.buttonBox.setObjectName("buttonBox")
        self.gridLayout.addWidget(self.buttonBox, 1, 0, 1, 1)

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

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Dialog", None, QtGui.QApplication.UnicodeUTF8))
and here is the main application :
Code:
#!/usr/bin/python
# -*- 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):
        QtGui.QSyntaxHighlighter.__init__(self,edit)
 
    def highlightBlock(self, text):        
        myClassFormat = QtGui.QTextCharFormat()
        myClassFormat.setFontWeight(QtGui.QFont.Bold)
        myClassFormat.setForeground(QtCore.Qt.darkMagenta)
        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()
    ui.setupUi2(Dialog)
    Dialog.show()
    sys.exit(app.exec_())
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
Snouffelaire
LQ Newbie
 
Registered: Mar 2007
Location: France
Distribution: Ubuntu 9.04
Posts: 13

Original Poster
Rep: Reputation: 0
Ok, I finally found an answer... I forgot this little line :
Code:
self.highlighter.setDocument(self.textEdit.document())
that should be added in the setupUI2 function.

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

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
Snouffelaire
LQ Newbie
 
Registered: Mar 2007
Location: France
Distribution: Ubuntu 9.04
Posts: 13

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 :
http://groups.google.fr/group/comp.l...fc8b120cb5d686
 
  


Reply


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


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

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