PyQt4 highlighting

I am trying to add syntax highlighting to a text editor in PyQt4. I found an example in the documentation that works when compiled from C ++, but when I convert it to Python / PyQt it no longer works.

The piece of code that fails (doesn't highlight anything anymore):

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:

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

      

Does anyone know why this is not working?

Versions I'm using: Python 2.6.2, PyQt 4.4.4

+2


source to share


2 answers


Ok ... it turns out I wasn't going crazy, I was just using an outdated version of PyQt4.

For information, the version of PyQt4 that ships with Ubuntu 9.04 is 4.4.4, but this feature requires 4.5+.



I've upgraded to PyQt4 4.6 and it works great (plus 4.6 seems to have some new new features too).

+1


source


Save lineColor

somewhere (for example self.lineColor

). Otherwise, Python will discard the object when the method returns, instead format

using an invalid pointer.



0


source







All Articles