# Is it posible to move a widget inside an UserWidget via code?

**URL:** <https://forums.unrealengine.com/t/is-it-posible-to-move-a-widget-inside-an-userwidget-via-code/591849>\
**Category:** C++\
**Tags:** unreal-engine, Widget, UE4, question, CPP\
**Created:** [June 27, 2022, 6:10pm UTC](https://forums.unrealengine.com/t/is-it-posible-to-move-a-widget-inside-an-userwidget-via-code/591849 "2022-06-27T18:10:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pitikixo](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Pitikixo](https://forums.unrealengine.com/u/Pitikixo)\
**Post date:** [June 27, 2022, 6:10pm UTC](https://forums.unrealengine.com/t/is-it-posible-to-move-a-widget-inside-an-userwidget-via-code/591849/1 "2022-06-27T18:10:03Z")

</div>

I’m trying to make something quite simple, yet i can’t find any way to do so…  
I want to be able to move the position of an UButton inside of my UserWidget in runtime via code.  
Like, for example: I have my button in X(200)Y(200) and when I press another diferrent button (or the same, whatever) the SAME button moves to X(300)Y(300).  
I can’t have two diferent buttons and make 1 visible and the other hidden cause I need to keep the OnClick event the same.  
I’ve read the UButton doccumentation and there doesn’t even seems to exist a variable “position” or something like that.  
The further I could get was this:

 ![imagen](https://d3kjluh73b9h9o.cloudfront.net/original/4X/9/7/e/97ee7c93db410e7a9f757ccc868592fa3a00d7db.png)  
There is this variable called Slot which I guess is the position of the widget inside of the UserWidget (wild guess tbh), but I have no idea how to handle it.  
I would like to make this in C++ but I dont even know how to do it in Blueprint so any kind of help would be apreciated (^\_^)  
Thanks in advance.

---

<div class="post-metadata">

**Author:** ![Pitikixo](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Pitikixo](https://forums.unrealengine.com/u/Pitikixo)\
**Post date:** [June 27, 2022, 6:24pm UTC](https://forums.unrealengine.com/t/is-it-posible-to-move-a-widget-inside-an-userwidget-via-code/591849/2 "2022-06-27T18:24:33Z")

</div>

I’m still loking for the way to do this in C++ however I found the answer for blueprint method here: [Dynamically set widget position on canvas panel?](https://forums.unrealengine.com/t/dynamically-set-widget-position-on-canvas-panel/436026)  
I’ll post a picture of the blueprints nodes in case someone needs it, this nodes take the existing button (Button\_165) and every time it’s clicked, move its own position to (x+100,y)

 ![imagen](https://d3kjluh73b9h9o.cloudfront.net/original/4X/0/5/a/05a276b150835670b69773985a4c9ec6ca1002e2.png)

---

<div class="post-metadata">

**Author:** ![Chatouille](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/chatouille/32/1194622_2.png) [@Chatouille](https://forums.unrealengine.com/u/Chatouille)\
**Post date:** [June 28, 2022, 10:11am UTC](https://forums.unrealengine.com/t/is-it-posible-to-move-a-widget-inside-an-userwidget-via-code/591849/3 "2022-06-28T10:11:19Z")

</div>

```cpp
#include "Components/Widget.h"
#include "Components/CanvasPanelSlot.h"

//UWidget* Widget;
auto Slot = Cast<UCanvasPanelSlot>(Widget->Slot);
Slot->SetPosition(Slot->GetPosition() + FVector2D(100, 0));

```

---

<div class="post-metadata">

**Author:** ![Pitikixo](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Pitikixo](https://forums.unrealengine.com/u/Pitikixo)\
**Post date:** [June 28, 2022, 10:43am UTC](https://forums.unrealengine.com/t/is-it-posible-to-move-a-widget-inside-an-userwidget-via-code/591849/4 "2022-06-28T10:43:05Z")

</div>

I guess I understood everything, thank you for your comment.  
But when I used it on my code like: (_ **Respuesta1** _ is the button i want to move)

```auto
auto Slot = Cast<UCanvasPanelSlot>(Respuesta1->Slot);
Slot->SetPosition(Slot->GetPosition() + FVector2D(100, 0));

```

I get this error log:

 ![imagen](https://d3kjluh73b9h9o.cloudfront.net/original/4X/0/4/1/0419fda0ef2dd4b5c3c9e5faa5dcbb7758c91e61.png)  
I have no idea what “auto” does, for what I’ve could find is like an automatic variable type but I might be wrong.

---

<div class="post-metadata">

**Author:** ![BrUnO\_XaVIeR](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/bruno_xavier/32/1110230_2.png) [@BrUnO\_XaVIeR](https://forums.unrealengine.com/u/BrUnO_XaVIeR)\
**Post date:** [June 28, 2022, 11:57am UTC](https://forums.unrealengine.com/t/is-it-posible-to-move-a-widget-inside-an-userwidget-via-code/591849/5 "2022-06-28T11:57:44Z")

</div>

None of that is necessary.  
Use widget animation feature of UMG.

---

<div class="post-metadata">

**Author:** ![Chatouille](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/chatouille/32/1194622_2.png) [@Chatouille](https://forums.unrealengine.com/u/Chatouille)\
**Post date:** [June 28, 2022, 12:49pm UTC](https://forums.unrealengine.com/t/is-it-posible-to-move-a-widget-inside-an-userwidget-via-code/591849/6 "2022-06-28T12:49:20Z")

</div>

It’s just that the variable name “Slot” is already taken by your current class probably, name it something else.  
`auto` simply tells the compiler to infer the type based on context, in this case UCanvasPanelSlot\*.

```cpp
UCanvasPanelSlot* TheSlot = Cast<UCanvasPanelSlot>(Respuesta1->Slot);
TheSlot->SetPosition(TheSlot->GetPosition() + FVector2D(100, 0));

```
