[PySide] Possbile to syntax hightlight the content of a QTreeWidget item?

ZHONG Zhu Zhu.Zhong at alcatel-sbell.com.cn
Fri Jan 25 02:43:30 CET 2013


Thank you Aaron, that works!

From: pyside-bounces+zhu.zhong=alcatel-sbell.com.cn at qt-project.org [mailto:pyside-bounces+zhu.zhong=alcatel-sbell.com.cn at qt-project.org] On Behalf Of Aaron Richiger
Sent: Thursday, January 24, 2013 6:36 PM
To: pyside at qt-project.org
Subject: Re: [PySide] Possbile to syntax hightlight the content of a QTreeWidget item?

Hello Zhu!

For QTreeWidget, you can use setItemWidget() to set any widget you want for this item. I used it to place a QLabel with colorized text in the item. To colorize the text of a QLabel you can use html. To generate the correct html for any assignment statement with binary operations of any variable, I implemented a very simple AST model, I guess you have something similar for your code. All nodes have a to_html() method to get colorized html output for this node. Here is a simple working example:

################## Code #################
#!/usr/bin/python

import sys
from PySide.QtGui import *


class AstNode(object):
    """Base class for all nodes of the code model."""

    def __init__(self):
        self.color = "black"

    def get_colorized_html_for(self, text):
        """Embed text in html tags setting the text font."""

        return '<font color="%s">%s</font>' % (self.color, text)


class Assignment(AstNode):
    """Model for an assignment statement."""

    def __init__(self, left, right):
        super(Assignment, self).__init__()
        self.left = left
        self.right = right

    def to_html(self):
        """Return html text to represent this node with syntax highlighting."""

        return self.left.to_html() + \
                self.get_colorized_html_for(" = ") + \
                self.right.to_html()


class BinaryOp(AstNode):
    """Model for a binary operation, such as a + b."""

    def __init__(self, left, right, operator):
        super(BinaryOp, self).__init__()
        self.color = "#0000FF"         # example for hex rgb, analogous to blue
        self.left = left
        self.right = right
        self.operator = operator

    def to_html(self):
        """Return html text to represent this node with syntax highlighting."""

        return self.left.to_html() + \
                self.get_colorized_html_for(' ' + self.operator + ' ') + \
                self.right.to_html()


class Variable(AstNode):
    """Model for a variable."""

    def __init__(self, name):
        super(Variable, self).__init__()
        self.name = name
        self.color = "red"

    def to_html(self):
        """Return html text to represent this node with syntax highlighting."""

        return self.get_colorized_html_for(self.name)


class TreeWidgetExample(QTreeWidget):

    def __init__(self, parent=None):
        super(TreeWidgetExample, self).__init__(parent)
        self.setHeaderLabels(["Action", "Parameter"])
        self.setColumnCount(2)
        root = QTreeWidgetItem(self, ["function"])
        children = QTreeWidgetItem(root, ["eval"])

        # Build code model for $result = $a + $b
        result_var = Variable("$result")
        a_var = Variable("$a")
        b_var = Variable("$b")
        addition = BinaryOp(a_var, b_var, "+")
        assignment = Assignment(result_var, addition)

        # Set a QLable as ItemWidget for the code column
        self.setItemWidget(children, 1, QLabel(assignment.to_html(), self))


def main():

    app = QApplication(sys.argv)
    ex = TreeWidgetExample()
    ex.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

#################### End Code ##################



Am 24.01.2013 04:03, schrieb ZHONG Zhu:
I'm using QTreeWidget to parse/show/edit one of our internal "language". Now user wants to have syntax highlight feature. I was able to highlight the whole item, for instance, the "comment" Action and the whole Parameter.

When it comes to the content within an item, such as "$result = $a + $b", I can't find a way to highlight individual variables ($result/$a/$b). Anyone has any experience on this? How? Thanks in advance!

BR

Zhu
[cid:image001.jpg at 01CDFAE0.6EB0C7F0]





_______________________________________________

PySide mailing list

PySide at qt-project.org<mailto:PySide at qt-project.org>

http://lists.qt-project.org/mailman/listinfo/pyside

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/pyside/attachments/20130125/a270e8b2/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image001.jpg
Type: image/jpeg
Size: 8789 bytes
Desc: image001.jpg
URL: <http://lists.qt-project.org/pipermail/pyside/attachments/20130125/a270e8b2/attachment.jpg>


More information about the PySide mailing list