已解决:移动项目的问题



是否有任何方法可以从当前位置移动一个qrect对象,有许多函数(moveTo, movelleft, ....),但它们都是从(0,0)移动对象,而不是从当前位置移动对象,如果我需要在x方向上将对象从当前位置移动5,可用的方法首先将其移动到(0,0),然后移动到(5,0);但是我需要从它的实际位置

移动它下面是代码:
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);

只用QRect::translate。在你的特殊情况下,可能是这样的…

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

或者,如果您想保持原始的QRect不被修改…

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

最新更新