# Pick a widget

**URL:** https://discourse.vtk.org/t/pick-a-widget/2573
**Category:** Support
**Created:** [February 10, 2020, 1:22pm UTC](https://discourse.vtk.org/t/pick-a-widget/2573 "2020-02-10T13:22:38Z")
**Posts on this page:** 18
**Page:** 1

<div class="post-metadata">

### Author: ![flaviu2](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/f/74df32/32.png) [@flaviu2](https://discourse.vtk.org/u/flaviu2)
#### Post date: [February 10, 2020, 1:22pm UTC](https://discourse.vtk.org/t/pick-a-widget/2573/1 "2020-02-10T13:22:38Z")

</div>

I am struggle for a while to achieve something simple: to pick a widget (a distance widget) with mouse pointer, **in order to modify it or delete it**.

Here is my trial:

```
	vtkPropPicker* pPicker = vtkPropPicker::New();
	pPicker->Pick(nPosition[0], nPosition[1], 0, m_pRenderer);
	TRACE("Pointer: %p\n", pPicker->GetProp3D());

```

when I hit the 2d image, I got:

```
Pointer: 0000020108617B10
Pointer: 0000020108617B10

```

but when I hit the widget handles (see red arrow):

![image](https://discourse.vtk.org/uploads/default/original/2X/c/c6744b6b8951fe79b2ba23f76b97b0ce9a80ecc4.jpeg)

my code is not executed at all … obviously I don’t understand why … can you tell me how to solve this task ? I have worked hard for this task and I didn’t succeeded by now …

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [February 10, 2020, 1:53pm UTC](https://discourse.vtk.org/t/pick-a-widget/2573/2 "2020-02-10T13:53:40Z")

</div>

Hello, try posting more of your code. Surely there is some `if()` there to explain why the TRACE() macro does not work when clicking the distance widget.

---

<div class="post-metadata">

### Author: ![flaviu2](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/f/74df32/32.png) [@flaviu2](https://discourse.vtk.org/u/flaviu2)
#### Post date: [February 10, 2020, 3:04pm UTC](https://discourse.vtk.org/t/pick-a-widget/2573/3 "2020-02-10T15:04:28Z")

</div>

Is hard to reproduce my situation in a test app, but if is need it, I would do it, because I am stuck here for days ☹

My pipe line looks like this: I have a **vtkRenderWindowInteractor** from over I setup

```
m_pInteractor->SetInteractorStyle(m_pInteractorStyleImage);

```

where **m\_pInteractorStyleImage** is **vtkInteractorStyleImageEx** type.

and _vtkInteractorStyleImageEx_ is

```
class vtkInteractorStyleImageEx : public vtkInteractorStyleImage

```

ok.

In this **vtkInteractorStyleImageEx** I got:

```
void vtkInteractorStyleImageEx::OnLeftButtonDown()
{
	int nPickPosition[2];
    this->Interactor->GetEventPosition(nPickPosition);
	vtkPropPicker* pPicker = vtkPropPicker::New();
	pPicker->Pick(nPickPosition[0], nPickPosition[1], 0, m_pView->m_pRenderer);
	TRACE("Pointer: %p\n", pPicker->GetProp3D());
	pPicker->Delete();

    vtkInteractorStyleImage::OnLeftButtonDown();
}

```

that is my testing code …

---

<div class="post-metadata">

### Author: ![flaviu2](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/f/74df32/32.png) [@flaviu2](https://discourse.vtk.org/u/flaviu2)
#### Post date: [February 10, 2020, 3:13pm UTC](https://discourse.vtk.org/t/pick-a-widget/2573/4 "2020-02-10T15:13:15Z")

</div>

The point is that I got any pointer when I click on 2d image, but if I click on widget handles, the code is not executed at all.

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [February 10, 2020, 4:18pm UTC](https://discourse.vtk.org/t/pick-a-widget/2573/5 "2020-02-10T16:18:19Z")

</div>

But if you click on the widget’s handles instead on the image, that is the expected behavior, right? I fail to understand the issue.

---

<div class="post-metadata">

### Author: ![flaviu2](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/f/74df32/32.png) [@flaviu2](https://discourse.vtk.org/u/flaviu2)
#### Post date: [February 10, 2020, 4:41pm UTC](https://discourse.vtk.org/t/pick-a-widget/2573/6 "2020-02-10T16:41:22Z")

</div>

I put a widget over my 2d image. That code, from here:

[https://discourse.vtk.org/t/pick-a-widget/2573/3?u=flaviu2](https://discourse.vtk.org/t/pick-a-widget/2573/3)

is working any time when I didn’t clicked on widget handles. When I clicked on widget handles, that code ( **vtkInteractorStyleImageEx::OnLeftButtonDown** )is not executed at all. And my task is to select that widget in order to change his color, or, to delete it.

So, the issue is: why that code is not executed at click on widget handles ?

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [February 10, 2020, 5:38pm UTC](https://discourse.vtk.org/t/pick-a-widget/2573/7 "2020-02-10T17:38:56Z")

</div>

I see…

You need to add callbacks to the widget’s interactor. Callbacks are pieces of your own code that are callable from elsewhere.

Try this:

```auto

void widgetPickObserver(vtkObject* caller, unsigned long eventId, void* clientdata, void *calldata)
{
   TRACE( "Key code is :%i\n", (static_cast<vtkRenderWindowInteractor*>(caller))->GetKeySym() );
   vtkDistanceWidget* distanceWidget = dynamic_cast<vtkDistanceWidget*>( clientdata );
   if( distanceWidget ){
        /** Good: clicked on the distance widget. */
   } else {
        /** Error: clicked something else. */
   }
}

void MyClass::createDistanceWidget()
{
  distanceWidget = vtkDistanceWidget::New();
  (...)

  vtkSmartPointer<vtkCallbackCommand> cb = vtkSmartPointer<vtkCallbackCommand>::New();
  cb->SetCallback(widgetPickObserver);
  cb->SetClientData(distanceWidget);
  distanceWidget->GetInteractor()->AddObserver(vtkCommand::KeyPressEvent, cb);

  (...)
  distanceWidget->On();
} 

```

---

<div class="post-metadata">

### Author: ![flaviu2](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/f/74df32/32.png) [@flaviu2](https://discourse.vtk.org/u/flaviu2)
#### Post date: [February 11, 2020, 7:20am UTC](https://discourse.vtk.org/t/pick-a-widget/2573/8 "2020-02-11T07:20:46Z")

</div>

Hi Paulo.

I have wrote:

```
void CVTKDoc::CreateDistanceWidget(CVTKView* pView)
{
	vtkDistanceWidget* pDistanceWidget = vtkDistanceWidget::New();
	pDistanceWidget->SetInteractor(pView->m_pInteractor);
	// Create the default widget representation and set the label format
	pDistanceWidget->CreateDefaultRepresentation();
	static_cast<vtkDistanceRepresentation*>(pDistanceWidget->GetRepresentation())->SetLabelFormat(_T("%-#6.3g mm"));
	// Allow the observer to access the widget
	vtkCallbackCommand* pKeypressCallback = vtkCallbackCommand::New();
	pKeypressCallback->SetCallback(&CVTKDoc::KeypressCallbackFunction);
	pKeypressCallback->SetClientData(pDistanceWidget);
	pDistanceWidget->GetInteractor()->AddObserver(vtkCommand::KeyPressEvent, pKeypressCallback);
	pDistanceWidget->On();
	// Add widget pointer to map to cleanup later
	if (NULL != pDistanceWidget)
		InsertWidgetToMap(pView, pDistanceWidget);
	pKeypressCallback->Delete();
}

```

and then (static method)

```
void CVTKDoc::KeypressCallbackFunction(vtkObject* caller, long unsigned int eventId, void* clientData, void* callData)
{
	CVTKDoc* pDoc = CVTKDoc::GetDoc();
	vtkRenderWindowInteractor* pInteractor = static_cast<vtkRenderWindowInteractor*>(caller);
	CString sKey = pInteractor->GetKeySym();
	TRACE(">>>%s\n", sKey);
}

```

and I see every keyboard key pressed. In order to select a widget (to modify it or delete it) I change the event, from **vtkCommand::KeyPressEvent** to **vtkCommand::LeftButtonPressEvent**. Every time when I press the left button, _I see the TRACE_, but when I press the button on the _widget handle_, the callback function is not executed at all, just like in the first solution ☹

---

<div class="post-metadata">

### Author: ![flaviu2](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/f/74df32/32.png) [@flaviu2](https://discourse.vtk.org/u/flaviu2)
#### Post date: [February 11, 2020, 2:54pm UTC](https://discourse.vtk.org/t/pick-a-widget/2573/10 "2020-02-11T14:54:27Z")

</div>

I guess this task (to select an widget in order to modify it or to delete it) is basic functionality … am I right ?

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [February 12, 2020, 11:14am UTC](https://discourse.vtk.org/t/pick-a-widget/2573/11 "2020-02-12T11:14:10Z")

</div>

I see, friend. But I think you’re beeing a bit over-precise on this. If I were your user, the last place I’d click on to delete the ruler widget would be its handles. I’d just click anywhere else and hit DEL. The widget handles are meant to adjust it, thus, your callback being not triggered is the expected behavior.

---

<div class="post-metadata">

### Author: ![flaviu2](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/f/74df32/32.png) [@flaviu2](https://discourse.vtk.org/u/flaviu2)
#### Post date: [February 12, 2020, 12:13pm UTC](https://discourse.vtk.org/t/pick-a-widget/2573/12 "2020-02-12T12:13:33Z")

</div>

Good observation. Thank you. I wonder if I could know if user press on widget body and not on 2d image, because the widget is spread over this 2d image …

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [February 12, 2020, 12:36pm UTC](https://discourse.vtk.org/t/pick-a-widget/2573/13 "2020-02-12T12:36:47Z")

</div>

But you’ve just said this:

> [@flaviu2](#):
>
> I change the event, from **vtkCommand::KeyPressEvent** to **vtkCommand::LeftButtonPressEvent**. Every time when I press the left button, _I see the TRACE_ ,

I understood this as you were able to respond to a click event on the widget. 🤔

---

<div class="post-metadata">

### Author: ![flaviu2](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/f/74df32/32.png) [@flaviu2](https://discourse.vtk.org/u/flaviu2)
#### Post date: [February 12, 2020, 12:38pm UTC](https://discourse.vtk.org/t/pick-a-widget/2573/14 "2020-02-12T12:38:57Z")

</div>

Yes, in did, but I have to know when user has been clicked on widget line, not anywhere on 2d image … you know what I mean ?

Later edit: **vtkCommand::LeftButtonPressEvent** is fired every time when I click on 2d image, not only on distance widget.

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [February 12, 2020, 12:43pm UTC](https://discourse.vtk.org/t/pick-a-widget/2573/15 "2020-02-12T12:43:45Z")

</div>

Recall this line of code:

> [@flaviu2](#):
>
> ```
> pDistanceWidget->GetInteractor()->AddObserver(vtkCommand::LeftButtonPressEvent, pKeypressCallback);
> 
> ```

If the callback is being triggered by a click on another object, then you should report a bug.

---

<div class="post-metadata">

### Author: ![flaviu2](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/f/74df32/32.png) [@flaviu2](https://discourse.vtk.org/u/flaviu2)
#### Post date: [February 13, 2020, 1:05pm UTC](https://discourse.vtk.org/t/pick-a-widget/2573/16 "2020-02-13T13:05:39Z")

</div>

> [@Paulo\_Carvalho](#):
>
> Error: clicked something else

Thank you Paulo, I reported a bug: [https://gitlab.kitware.com/vtk/vtk/issues/17784](https://gitlab.kitware.com/vtk/vtk/issues/17784)

---

<div class="post-metadata">

### Author: ![flaviu2](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/f/74df32/32.png) [@flaviu2](https://discourse.vtk.org/u/flaviu2)
#### Post date: [February 19, 2020, 6:53am UTC](https://discourse.vtk.org/t/pick-a-widget/2573/17 "2020-02-19T06:53:49Z")

</div>

The bug has been closed because is not a bug. It only need to catch accurate the line of the widget.

Thank you a lot Paulo !!!

P.S. Because I picked in my mouse pointer the _vtkDistanceRepresentation2D_ object, I have to see now how can I get the _vtkDistanceWidget_ pointer which contain this picked _vtkDistanceRepresentation2D_.

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [February 19, 2020, 11:33am UTC](https://discourse.vtk.org/t/pick-a-widget/2573/18 "2020-02-19T11:33:02Z")

</div>

Well, if it is not a bug, then you have to carefully review your code as the callback is surelly being triggered by a click on the widget. I can’t see why this is wrong. Just click anywhere in the line to select it, just that. I don’t know why you insist in trying to select it by it’s tiny resizing handles located in the very ends of the widget… Like I said before: the last place I would click to select a widget would be by its resizing handles.

---

<div class="post-metadata">

### Author: ![flaviu2](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/f/74df32/32.png) [@flaviu2](https://discourse.vtk.org/u/flaviu2)
#### Post date: [February 19, 2020, 11:50am UTC](https://discourse.vtk.org/t/pick-a-widget/2573/19 "2020-02-19T11:50:22Z")

</div>

No no, I have picked only when I click on his line, not on his handle. Which is ok.
