# How to automatically associate a selected model with a selected QtComponentTreeItem in Visualize HPS?

**URL:** <https://forum.techsoft3d.com/t/how-to-automatically-associate-a-selected-model-with-a-selected-qtcomponenttreeitem-in-visualize-hps/2870>\
**Category:** HOOPS Visualize Desktop\
**Created:** [November 9, 2023, 10:30am UTC](https://forum.techsoft3d.com/t/how-to-automatically-associate-a-selected-model-with-a-selected-qtcomponenttreeitem-in-visualize-hps/2870 "2023-11-09T10:30:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rui.Zhang03](https://avatars.discourse-cdn.com/v4/letter/r/898d66/32.png) [@Rui.Zhang03](https://forum.techsoft3d.com/u/Rui.Zhang03)\
**Post date:** [November 9, 2023, 10:30am UTC](https://forum.techsoft3d.com/t/how-to-automatically-associate-a-selected-model-with-a-selected-qtcomponenttreeitem-in-visualize-hps/2870/1 "2023-11-09T10:30:32Z")

</div>

How to automatically associate a selected model with a selected QtComponentTreeItem in Visualize HPS?

 ![企业微信截图_16995256697482](https://us1.discourse-cdn.com/flex020/uploads/techsoft3d/original/2X/1/1ec4e15b84d1adcc7aae93ec5835dbb4c8400d4b.png)

I’m trying to traverse the QTreeWidget, comparing to HPS::ComponentPath, but it often reports an invalid memory access error directly on mouse click on the model, and I can’t locate the exception!

hps\_qt\_sandbox includes the QtComponentTreeItem highlighting model, but does not include the ability to select a node with the mouse, and the component tree can be linked to select it.

---

<div class="post-metadata">

**Author:** ![Rui.Zhang03](https://avatars.discourse-cdn.com/v4/letter/r/898d66/32.png) [@Rui.Zhang03](https://forum.techsoft3d.com/u/Rui.Zhang03)\
**Post date:** [November 9, 2023, 10:33am UTC](https://forum.techsoft3d.com/t/how-to-automatically-associate-a-selected-model-with-a-selected-qtcomponenttreeitem-in-visualize-hps/2870/2 "2023-11-09T10:33:02Z")

</div>

Using HPS::HighlightOperator to select a model does not reverse lookup to the corresponding QtComponentTreeItem.

---

<div class="post-metadata">

**Author:** ![Rui.Zhang03](https://avatars.discourse-cdn.com/v4/letter/r/898d66/32.png) [@Rui.Zhang03](https://forum.techsoft3d.com/u/Rui.Zhang03)\
**Post date:** [November 9, 2023, 10:35am UTC](https://forum.techsoft3d.com/t/how-to-automatically-associate-a-selected-model-with-a-selected-qtcomponenttreeitem-in-visualize-hps/2870/3 "2023-11-09T10:35:07Z")

</div>

I used the following code for the reverse lookup  
void SelectTreeItemByPath(QTreeWidgetItem\* treeItem, HPS::ComponentPath path, QTreeWidgetItem\*& findItem)  
{  
size\_t childNums = treeItem-\>childCount();  
for (size\_t i = 0; i \< childNums; i++)  
{  
QTreeWidgetItem\* child = treeItem-\>child(i);  
child-\>setSelected(false);  
QtComponentTreeItem\* treeItem = child-\>data(0, Qt::ItemDataRole::UserRole).value\<QtComponentTreeItem\*\>();  
treeItem-\>getTreeItem()-\>setSelected(false);  
if (treeItem-\>GetPath() == path)  
{  
findItem = child;  
child-\>setSelected(true);  
}  
}  
for (size\_t i = 0; i \< childNums; i++)  
{  
QTreeWidgetItem\* child = treeItem-\>child(i);  
SelectTreeItemByPath(child, path, findItem);  
}  
}

---

<div class="post-metadata">

**Author:** ![gabriel.peragine](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.techsoft3d.com/gabriel.peragine/32/1535_2.png) [@gabriel.peragine](https://forum.techsoft3d.com/u/gabriel.peragine)\
**Post date:** [November 10, 2023, 5:09pm UTC](https://forum.techsoft3d.com/t/how-to-automatically-associate-a-selected-model-with-a-selected-qtcomponenttreeitem-in-visualize-hps/2870/4 "2023-11-10T17:09:41Z")

</div>

Hey Rui, thanks for sharing your code. I think this might be better answered by support, so I’ll make you a ticket and respond here when I get an answer.

I think @dave.calkins or @jamestruax might have some insight here as they are both very well versed in Visualize and maybe they have some pointers.

---

<div class="post-metadata">

**Author:** ![gabriel.peragine](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.techsoft3d.com/gabriel.peragine/32/1535_2.png) [@gabriel.peragine](https://forum.techsoft3d.com/u/gabriel.peragine)\
**Post date:** [November 15, 2023, 4:56pm UTC](https://forum.techsoft3d.com/t/how-to-automatically-associate-a-selected-model-with-a-selected-qtcomponenttreeitem-in-visualize-hps/2870/5 "2023-11-15T16:56:58Z")

</div>

Here’s some information as provided by support. Hopefully it is helpful.:

As for associating a QtComponentTreeItem with HPS, you will need to call HPS::Component::.GetKeys(). This will get you the HPS KeyPath associated with the HOOPS Exchange Component.

More information can be found here:  
[https://docs.techsoft3d.com/hps/latest/prog\_guide/0906\_exchange\_integration.html#introduction](https://docs.techsoft3d.com/hps/latest/prog_guide/0906_exchange_integration.html#introduction)
