delphinodestvirtualstringtree

Can VirtualStringTree avoid attaching data to a Node?


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.


Solution

  • 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).