1
votes

A standard VirtualStringTree in the examples I've seen has a GetText procedure to get at data which is stored in memory via InitNode. It uses a set of Nodes to manage display, ordering etc.

An application which collects and manages some data in its own class (TMyDataClass) but uses a VirtualStringTree just to display it could, however, simply access TMyDataClass's data in GetText without bothering to attach that data to a Node in InitNode (and later freeing it in FreeNode).

I'm trying to understand any downside there is to that. What functionality of VirtualStringTree will I miss if I don't use InitNode and FreeNode? I have nothing against those of course but I just want to understand better.

1
If you don't use node data, how to you link data in your class instance to a tree node? How do you write GetText so that it gets data? There is no need to duplicate data from your TMyDataClass to node data: node data should only contain a pointer to TMyDataClass instance and anything require (such an index or another pointer) to find data in the class. - fpiette
Simply CellText := TMyDataClass.Names[Node.Index] (for example) - Mike Scott
Node.Index is index of node with regard to its parent node and as such can't be used like you show. And probably Node.Index change as node are moved around, deleted or inserted. - fpiette

1 Answers

3
votes

The TVirtualStringTree is used to handle elements related to display wihtout worrying about what the data actually is.

To allow it to do that, each of the Node records can store additional information to get to the data it needs to know. The Data property. In general you don't want to have multiple copies of your data, so in the Data property of the Node you would store a pointer (reference) to your object from which it can get the data. I understand that the Data property is defined as a record - so you need to define a record to hold your data. (Please note I may be referring to a different version than you are using - check your docs or source code to confirm what you need).

So that would mean you have, for example:

  CellText := TMyNodeRecord(Node.Data).AppData.DisplayText;

Where TMyNodeRecord is defined as something like:

  type TMyNodeRecord = record
    AppData: TMyDataClass;
  end;

Where TMyDataClass is your own data object that supplies the text through the DisplayText property (or any other property / metod you like).