将数据写入PVirtualNode,无需手动设置各个字段值
假设我有这个节点数据记录:
Type
PPerson = ^TPerson;
TPerson = record
Name: String;
Age: Integer;
SomeBool: Boolean;
end;
要填充我的 VirtualStringTree,我会这样做:
Procedure AddToTree(Person: TPerson);
Var
Node: PVirtualNode;
Data: PPerson;
Begin
Node := VT.AddChild(nil);
Data := VT.GetNodeData(Node);
Data.Name := Person.Name;
Data.Age := Person.Age;
Data.SomeBool := Person.SomeBool;
End;
Procedure TMyForm.MyButtonClick(Sender: TObject);
Var
Person: TPerson;
Begin
Person.Name := 'Jeff';
Person.Age := 16;
Person.SomeBool := False;
AddToTree(Person);
End:
现在,虽然这工作得很好,但我想简化它,所以每当我向记录添加新字段时,我都不会修改 AddToTree方法。
所以我尝试了这个:
Procedure AddToTree(Person: TPerson);
Begin
VT.AddChild(nil,@Person);
End;
这个编译,但是似乎PVirtualNode
没有获取数据,因为我的VT没有显示任何内容,当中断OnGetText事件时,我看到变量是空的。
我做错了什么? :)
Lets say I have this node data record:
Type
PPerson = ^TPerson;
TPerson = record
Name: String;
Age: Integer;
SomeBool: Boolean;
end;
To populate my VirtualStringTree, I would do this:
Procedure AddToTree(Person: TPerson);
Var
Node: PVirtualNode;
Data: PPerson;
Begin
Node := VT.AddChild(nil);
Data := VT.GetNodeData(Node);
Data.Name := Person.Name;
Data.Age := Person.Age;
Data.SomeBool := Person.SomeBool;
End;
Procedure TMyForm.MyButtonClick(Sender: TObject);
Var
Person: TPerson;
Begin
Person.Name := 'Jeff';
Person.Age := 16;
Person.SomeBool := False;
AddToTree(Person);
End:
Now, while this works perfectly fine, I would like to simplify it, so whenever I add new fields to the record, I wont have modify the AddToTree method.
So I tried this:
Procedure AddToTree(Person: TPerson);
Begin
VT.AddChild(nil,@Person);
End;
This compiles, but it appears the PVirtualNode
did not get the data, because my VT is not displaying anything, and when breaking in the OnGetText event, I see the variables are empty.
What am I doing wrong? :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
记录支持赋值运算符:
Records support the assignment operator:
您没有阅读手册 :)
好的,在本例中,来源是手册 - 引用
AddChild()
来源:IOW,它并不意味着按照您使用它/期望它工作的方式使用。
顺便说一句,你为什么要复制数据?为什么不使用
New()
分配记录并将它们保留在树中,然后在树被清除时使用Dispose()
呢?You aren't reading the manual :)
OK, in this case the source is the manual - quote from the
AddChild()
source:IOW it isn't meant to be used in the way youre using it / expecting it to work.
BTW why do you copy data around? Why not have
and allocate records with
New()
keep them in the tree and thenDispose()
when tree is cleared?使用记录作为数据持有者时,在 VTV 中存储数据的最佳方法是仅存储指向记录的指针,而记录本身单独存储在列表/数组中。当可视组件实际上并不拥有数据时,这也对应于虚拟和 MVC 范例。
IOW,添加记录的方案是:
以及删除方案一条记录是:
引用计数字段
The best way of storing data in VTV when using records as data holders is to store only pointers to the records while records themselves are stored separately in a list/array. This also corresponds to a virtual and MVC paradigm when visual component doesn't actually owns data.
IOW, the scheme of adding a record is:
and the scheme of deleting a record is:
ref-counted fields
还有一个“我在问问题后 2 分钟就找到了答案”——多么丢人……:(
无论如何,所以——这可以通过使用 CopyMemory 来完成,如下所示:
Yet another "I found the answer 2 minutes after I asked the question" - how humiliating... :(
Anyways, so - this can be acomplished by using CopyMemory, like this: