[SOLVED] Porting Qt highlighting example code to PyQt4
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
[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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.