|
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:
|
_______________________________________________ PySide mailing list [email protected] http://lists.qt-project.org/mailman/listinfo/pyside
