Search code examples
delphidelphi-xe2virtualtreeviewtvirtualstringtree

TVirtualStringTree - add object instead of record


I have a TVirtualStringTree(version 5.2.2) and I need to add to it's nodes objects instead of records.

I have already looked at the answers provided on the question: Delphi, VirtualStringTree - classes (objects) instead of records but they are not working.

Objects I want to add to the tree's nodes are like

  TNodeElem = class
  public
    LineTimeS, LogMessage, MethName: String;
    LineDate,LineTime: TDateTime;
    LineDateTime: TDateTime;
    ElemType: TLogLineType;//ordinal type
....
  end;

these nodes are added to a TObjectList:

FObjLst.Add(lNode);

and added to the tree:

var iPos: Integer;
    lNode: PVirtualNode;
    ldata: TNodeElem;
begin
  FTreeView.BeginUpdate;
  for iPos := 0 to FObjLst.Count -1 do
  begin
   lNode := FTreeView.AddChild(nil);
   lData := TNodeElem(FObjLst[iPos]);
   FTreeView.getNodeData(lNode)^ := lData;//E2015 Operator not aplicable to this operand type
   FTreeView.ValidateNode(lNode,False);
  end;
  FTreeView.EndUpdate;
end;

procedure VSTGetText(Sender: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex;
    TextType: TVSTTextType; var CellText: UnicodeString);
var
 Data: TNodeElem;
begin
 Data := FTreeView.GetNodeData(Node);
 CellText := Data.LogMessage;
end;

I get an E2015 Operator not aplicable to this operand type error . It is probably something extremely simple what I'm missing here...

I know I can add it as a record with an object member like:

TNode = record
 obj: TMyObject;
end;

but I want to understand what I'm doing wrong.


Solution

  • function AddItem(Item: TNodeElem): PVirtualNode;
    begin
      Result := FTreeView.InsertNode(nil, amAddChildLast, Item);
    end;
    
    function GetItem(Node: PVirtualNode): TNodeElem;
    var
      NodeData: Pointer;
    begin
      Result := nil;
      if not Assigned(Node) then
        exit;
      NodeData := FTreeView.GetNodeData(Node);
      if Assigned(NodeData) then
        Result := TNodeElem(NodeData^);
    end;