QtGui.QTextEdit 根据行包含的文本设置行颜色 [英] QtGui.QTextEdit set line color baced on what text the line contains

查看:45
本文介绍了QtGui.QTextEdit 根据行包含的文本设置行颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次使用 stackoverflow 为我的问题寻找答案.我正在使用 QtGui.QTextEdit 来显示类似于下面的文本,并希望根据它们是否包含某些文本来更改某些行上文本的颜色.

It's my first time using stackoverflow to find an answer, to my problems. I'm using a QtGui.QTextEdit to display text similar to below and would like to change the color of the text on some lines based on if they contain certain text.

以--[开头的行是蓝色的,包含[ERROR]的行是红色的.我目前有类似以下内容,

lines that start with --[ will be blue and lines that contain [ERROR] would be red. I currently have something like the following,

from PyQt4 import QtCore, QtGui, uic
import sys

class Log(QtGui.QWidget):
    def __init__(self, path=None, parent=None):
        QtGui.QMainWindow.__init__(self, parent)
        self.taskLog = QtGui.QTextEdit()
        self.taskLog.setLineWrapMode(False)
        vbox = QtGui.QVBoxLayout()
        vbox.addWidget(self.taskLog)
        self.setLayout(vbox)

        log = open("/net/test.log", 'r')
        self.taskLog.setText(log.read())
        log.close()


app = QtGui.QApplication(sys.argv)
wnd = Log()
wnd.show()
sys.exit(app.exec_())

文字现在看起来像这样

--[ Begin 
this is a test

[ERROR] this test failed.

--[ Command returned exit code 1

希望你们都能够帮助我更快地解决这个问题,并尝试自己解决.

Hopefully you all will be able to help me work this out a lot faster that, trying to work it out my self.

谢谢,标记

推荐答案

这可以通过 QSyntaxHighlighter.这是一个简单的演示:

This can be done quite easily with QSyntaxHighlighter. Here's a simple demo:

from PyQt4 import QtCore, QtGui

sample = """
--[ Begin
this is a test

[ERROR] this test failed.

--[ Command returned exit code 1
"""

class Highlighter(QtGui.QSyntaxHighlighter):
    def __init__(self, parent):
        super(Highlighter, self).__init__(parent)
        self.sectionFormat = QtGui.QTextCharFormat()
        self.sectionFormat.setForeground(QtCore.Qt.blue)
        self.errorFormat = QtGui.QTextCharFormat()
        self.errorFormat.setForeground(QtCore.Qt.red)

    def highlightBlock(self, text):
        # uncomment this line for Python2
        # text = unicode(text)
        if text.startswith('--['):
            self.setFormat(0, len(text), self.sectionFormat)
        elif text.startswith('[ERROR]'):
            self.setFormat(0, len(text), self.errorFormat)

class Window(QtGui.QWidget):
    def __init__(self):
        super(Window, self).__init__()
        self.editor = QtGui.QTextEdit(self)
        self.highlighter = Highlighter(self.editor.document())
        self.editor.setText(sample)
        layout = QtGui.QVBoxLayout(self)
        layout.addWidget(self.editor)

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.setGeometry(500, 150, 300, 300)
    window.show()
    sys.exit(app.exec_())

这篇关于QtGui.QTextEdit 根据行包含的文本设置行颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆