3

I need to create QTreeWidgetItems which have support for formatted texts, such as:

MyCreatedType - INTEGER(1)

(ie: the line above should have a "normal" part : MyCreatedType and a "formatted" part (INTEGER(1) in our case).

Any idea how to accomplish this?

Thanks.

Ferenc Deak
  • 30,889
  • 14
  • 82
  • 151
  • I don't quite understand the question - do you want to put numbers in the item text string? – badgerr Jul 06 '12 at 09:09
  • No, I just want that the text of the item can be formatted (ie: some parts of it will be with Bold characters, or using a different font, color, etc...). – Ferenc Deak Jul 06 '12 at 10:30
  • maybe a duplicate of [this question](http://stackoverflow.com/questions/1956542/how-to-make-item-view-render-rich-html-text-in-qt) ? – azf Jul 06 '12 at 11:30

1 Answers1

0

What you need is a delegate. Delegates are explained here:

The general procedure I follow when creating and using custom delegates:

  1. Create a custom type with the information you want to encapsulate.
    • For your case, perhaps fields for the variable type name and type value.
  2. Store these custom types in your model, wrapping them in QVariants to satisfy the return types required by QAbstractItemModel
  3. Create a control that matches the UI you want.
    • In this case it might mean a QText label for "MyCreatedType" followed by a second label in bold for "Integer(1)".
    • Perhaps the control has methods like "setTypeName" and "setTypeValue"
  4. Create a delegate that paints your specific control when your custom type is found.
    • You will have to map fields in the custom type to fields in the custom UI control as needed.
  5. Associate your model and delegate with the Tree View you are using.

I hope this general procedure makes sense. I would recommend completing the Star Delegate Example and then reading my procedure, as it will make more sense with some background.

ccozad
  • 1,104
  • 8
  • 13