Reputation: 12584
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.
Upvotes: 5
Views: 1258
Reputation: 224
Another way is to store index of object in Tlist. Then you don't work by reference. It's more easiest to check bad reference.
procedure TForm1.Button2Click(Sender: TObject);
var i : Integer;
begin
for i := 0 to Pred(Mylist.count) do
VirtualStringTree1.AddChild(nil, pInteger(i));
end;
procedure TForm1.VirtualStringTree1GetNodeDataSize(Sender: TBaseVirtualTree; var NodeDataSize: Integer);
begin
NodeDataSize := SizeOf(integer);
end;
procedure TForm1.VirtualStringTree1GetText(Sender: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex;
TextType: TVSTTextType; var CellText: string);
var i : Integer;
begin
if Node = nil then Exit;
CellText := 'Unknown';
i := pInteger(Sender.GetNodeData(node))^;
if (i >= 0) and (i < Mylist.Count) then begin
if Assigned(MyList[i]) then
Celltext := TmyObject(MyList[i]).MyText;
end;
end;
Upvotes: 0
Reputation: 1468
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;
Upvotes: 5