Jonathan
Jonathan

Reputation: 319

PyQt4 Highlighting

I'm trying to add some syntax highlighting to a text editor in PyQt4. I've found an example in the documentation which works find when compiled from C++ but when i convert it to Python/PyQt it no longer works.

The part of the code that fails (no longer highlights anything) is:

def highlightCurrentLine(self):

    extraSelections = []

    if not self.isReadOnly():
        selection = QTextEdit.ExtraSelection()

        lineColor = QColor(Qt.yellow).lighter(160)

        selection.format.setBackground(lineColor)
        selection.format.setProperty(QTextFormat.FullWidthSelection, QVariant(True))
        selection.cursor = self.textCursor()
        selection.cursor.clearSelection()
        extraSelections.append(selection)

    self.setExtraSelections(extraSelections)

which is called by:

self.connect(self, SIGNAL('cursorPositionChanged()'), self.highlightCurrentLine)

Anyone have any idea why this doesn't work?

The versions i am usuing are: Python 2.6.2, PyQt 4.4.4

Upvotes: 2

Views: 1905

Answers (2)

Jonathan
Jonathan

Reputation: 319

Ok... turns out i wasn't going mad, i was just using an out of date version of PyQt4.

For information the version of PyQt4 that ships with Ubuntu 9.04 is 4.4.4 but this functionality seems to require 4.5+.

I've upgraded to PyQt4 4.6 and it works fine (plus 4.6 seems to have some nice new functionality too).

Upvotes: 1

Aaron Digulla
Aaron Digulla

Reputation: 328574

Save lineColor somewhere (like self.lineColor). Otherwise, Python will discard the object when the method returns and the format will use an illegal pointer.

Upvotes: 0

Related Questions