更改QPlainTextEdit中某些行的背景颜色

时间:2015-03-07 18:13:09

标签: pyqt

这不起作用:

    def lineError(self):
        block= self.firstVisibleBlock()
        while block.isValid():
            if block.blockNumber() in self.lineError:
                block.blockFormat().setBackground(QBrush(self.errorColor))
                block.charFormat().setBackground(QBrush(self.errorColor))
            block= block.next()

你有什么想法吗?

1 个答案:

答案 0 :(得分:1)

正确的方式:

        fmt= QTextBlockFormat()
        fmt.setBackground(self.errorColor)
        while block.isValid():
            if block.blockNumber() in self.lineError:
                QTextCursor(block).setBlockFormat(fmt)
            block= block.next()