有人可以帮助我在C ++ Builder中使用VirtualTree吗?
我有VT1
,其中有记录:
还有VT2
,里面有记录:
我将值从VT1
复制到VT2
:
void __fastcall TForm3::CopyItemsFromVT1ToVT2(TVirtualStringTree *VT_List, PVirtualNode NodeList, TVirtualStringTree *VT_Tree, PVirtualNode NodeTree) {
((PTreeData)VT_Tree->GetNodeData(NodeTree))->Id=((TForm1::PTreeData)VT_List->GetNodeData(NodeList))->Id;
((PTreeData)VT_Tree->GetNodeData(NodeTree))->Type=((TForm1::PTreeData)VT_List->GetNodeData(NodeList))->Type;
((PTreeData)VT_Tree->GetNodeData(NodeTree))->Date=((TForm1::PTreeData)VT_List->GetNodeData(NodeList))->Date;
((PTreeData)VT_Tree->GetNodeData(NodeTree))->Time=((TForm1::PTreeData)VT_List->GetNodeData(NodeList))->Time;
((PTreeData)VT_Tree->GetNodeData(NodeTree))->Char_Code=((TForm1::PTreeData)VT_List->GetNodeData(NodeList))->Char_Code;
((PTreeData)VT_Tree->GetNodeData(NodeTree))->Number_Code=((TForm1::PTreeData)VT_List->GetNodeData(NodeList))->Number_Code;
((PTreeData)VT_Tree->GetNodeData(NodeTree))->Message=((TForm1::PTreeData)VT_List->GetNodeData(NodeList))->Message;
}
如何从VT1
到VT2
引用记录?这是正确的吗?
((PTreeData)VT2->GetNodeData(ParentNodeTarget)) = ((PTreeData)VT1->GetNodeData(ParentNodeSource))
答案 0 :(得分:0)
假设TreeData
是struct
或class
且实现了operator=
(而不是手动复制单个字段),则可以让编译器为您完成由编译器生成的默认值),例如:
struct TreeData
{
...
};
typedef TreeData *PTreeData;
...
VT1->NodeDataSize = sizeof(TreeData);
VT2->NodeDataSize = sizeof(TreeData);
...
void __fastcall TForm3::CopyItemsFromVT1ToVT2(TVirtualStringTree *VT_List, PVirtualNode NodeList, TVirtualStringTree *VT_Tree, PVirtualNode NodeTree)
{
*static_cast<PTreeData>(VT_Tree->GetNodeData(NodeTree)) = *static_cast<PTreeData>(VT_List->GetNodeData(NodeList));
}
但是,如果您想让VT2
个节点简单地指向与VT1
个节点相同的数据而不进行复制,那么您需要在任何节点之外分配TreeData
个实例,然后将TreeData*
指针存储在节点内部,例如:
struct TreeData
{
...
};
typedef TreeData *PTreeData, **PPTreeData;
...
VT1->NodeDataSize = sizeof(PTreeData);
VT2->NodeDataSize = sizeof(PTreeData);
...
PTreeData data = new TreeData;
// initialize data as needed ...
*static_cast<PPTreeData>(VT1->GetNodeData(SomeNode)) = data;
...
// OnFreeNode event handler for VT1...
void __fastcall TForm3::VT1FreeNode(TBaseVirtualTree *Sender, PVirtualNode Node)
{
delete *static_cast<PPTreeData>(Sender->GetNodeData(Node));
}
...
void __fastcall TForm3::CopyItemsFromVT1ToVT2(TVirtualStringTree *VT_List, PVirtualNode NodeList, TVirtualStringTree *VT_Tree, PVirtualNode NodeTree)
{
*static_cast<PPTreeData>(VT_Tree->GetNodeData(NodeTree)) = *static_cast<PPTreeData>(VT_List->GetNodeData(NodeList));
}