How to move an Item's QRect

179 Views Asked by At

Is there any way to move a QRect object from its current position?

There are many functions (moveTo, moveLeft, …), but all of them move the object from (0,0), and not from its current position.

If I need to move my object from its current position by 5 in X-direction, available methods moves it first to (0,0), and then to (5,0);

But I need to move it from its actual position.

int x_pos = item->rect.x();
int y_pos = item->rect.y();
x_pos -= 10;
y_pos -= 10;

item->rect.moveTo(x_pos, y_pos);
item->rect.setX(x_pos);
item->rect.setY(y_pos);
1

There are 1 best solutions below

0
On

Just use QRect::translate. In your particular case that would be something like...

item->rect.translate(-10, -10);

Or, if you want to leave the original QRect unmodified...

auto new_rect = item->rect.translated(-10, -10);