Jump to content
Sign in to follow this  
shineworld

TreeView Child Node Data

Recommended Posts

In a Python program, with DelphiVCL, I'm trying to use the TreeNode.Data field to contain extra node infos.


 

        def add_node_recursive(treeview: TreeView, parent_node: TreeNode, data: dict):
            axis_label = ''
            axis_id = data['id']
            if axis_id in ['X', 'Y', 'Z', 'A', 'B', 'C']:
                axis_label = f'{axis_id}-axis'
            elif axis_id == 'TP':
                axis_label = f'Toolpath'
            elif axis_id == 'MCS':
                axis_label = f'Machine Zero Point'
            else:
                axis_label = f'{axis_id}'
            axis_node = treeview.Items.AddChild(parent_node, axis_label)
            print(type(axis_node))
            print(type(axis_node.Data))

            
[Dbg]>> print(type(axis_node))
<class 'TreeNode'>
[Dbg]>> print(type(axis_node.Data)) 
<class 'NoneType'>
[Dbg]>>> dir(axis_node)
['AbsoluteIndex', 'AfterConstruction', 'AlphaSort', 'Assign', 'BeforeDestruction', 'CPP_ABI_1', 'CPP_ABI_2', 'CPP_ABI_3', 'CheckState', 'Checked', 'ClassInfo', 'ClassName', 'ClassNameIs', 'ClassParent', 'ClassType', 'CleanupInstance', 'Collapse', 'Count', 'Create', 'CustomSort', 'Cut', 'Data', 'DefaultHandler', 'Delete', 'DeleteChildren', 'Deleting', 'Destroy', 'Dispatch', 'DisplayRect', 'DisposeOf', 'DropHighlighted', 'DropTarget', 'EditText', 'Enabled', 'EndEdit', 'Equals', 'Expand', 'Expanded', 'ExpandedImageIndex', 'FieldAddress', 'Focused', 'Free', 'FreeInstance', 'GetHandle', 'GetHashCode', 'GetInterface', 'GetInterfaceEntry', 'GetInterfaceTable', 'GetLastChild', 'GetNamePath', 'GetNext', 'GetNextChild', 'GetNextVisible', 'GetPrev', 'GetPrevChild', 'GetPrevVisible', 'Handle', 'HasAsParent', 'HasChildren', 'ImageIndex', 'Index', 'IndexOf', 'InheritsFrom', 'InitInstance', 'InstanceSize', 'IsFirstNode', 'IsVisible', 'Item', 'ItemId', 'Level', 'MakeVisible', 'MethodAddress', 'MethodName', 'MoveTo', 'NewInstance', 'OverlayIndex', 'Owner', 'Parent', 'QualifiedClassName', 'SafeCallException', 'Selected', 'SelectedIndex', 'SetProps', 'StateIndex', 'Text', 'ToList', 'ToString', 'ToTuple', 'TreeView', 'UnitName', 'UnitScope', '__bound__', '__class__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__owned__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'getFirstChild', 'getNextSibling', 'getPrevSibling']

Initially node Data is set to NoneType but If I try to assign something I get:
 

AttributeError: Error in setting property Data
Error: Expected a PPyObject

 

Share this post


Link to post
  • TTreeNode.Data is a Pointer property and WrapDelphi does not support raw pointer properties.
  • The only pointer property supported is PPyObject.   So if your wrapped class had a property declared as PPyObject it would work.  This also explains the error message.
  •  To be able to use the Data property you would need to modify the TTreeNode wrapper and do some custom wrapping of the TreeNode.Data property.   Alternatively you could save whatever info you need to store in a python data structure (possibly dict for easy access).
  • Thanks 1

Share this post


Link to post

In the latest commits, I have added the ability to handle pointer fields and properties.  They are converted to python integers.

 

So you could store for example an integer to the TTreeNode.Data.

You could also store pointers to python objects using ctypes.   

See for example the following:

from ctypes import py_object, pointer, addressof, cast, POINTER

# Create a py_object
original = "Hello"
py_obj = py_object(original)

# Get its address
addr = addressof(py_obj)  # e.g., 0x7f8b5c403020

# Recover the py_object
py_ptr = cast(addr, POINTER(py_object))
recovered_py_obj = py_ptr.contents
recovered = recovered_py_obj.value

print(recovered)  # "Hello"
print(original is recovered)  # True (same object)

addr is an int type (python integer) that can be stored in Delphi pointer property. 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×