题
我想添加一些语法高亮在PyQt4的文本编辑器。我发现它的工作原理发现在C编译时,文档中++的一个例子,但是当我将其转换到Python / PyQt的它不再起作用。
失败的代码(不再突出的任何东西)的部分是:
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)
这是由称为:
self.connect(self, SIGNAL('cursorPositionChanged()'), self.highlightCurrentLine)
任何人有任何想法,为什么这不起作用?
我正在usuing的版本是:Python之2.6.2,4.4.4 PyQt的
解决方案
好吧...原来我不是要疯了,我只是使用了的PyQt4中的最新版本。
有关信息PyQt4中的版本附带Ubuntu的9.04是4.4.4但此功能似乎需要4.5 +。
我已经升级到4.6 PyQt4的和它工作正常(加4.6似乎有一些不错的新功能太)。
其他提示
保存lineColor
某处(如self.lineColor
)。否则,Python将丢弃该对象时,方法返回与format
将使用一个非法的指针。
不隶属于 StackOverflow