簡體   English   中英

移動項目的 Qrect

[英]Move Item's Qrect

有什么方法可以從當前的 position 移動 qrect object,有很多函數(moveTo、moveLeft、....),但所有這些函數都將 object 從 (0,0) 移動,而不是從當前的 position 移動,如果我需要將我的 object 從其當前的 position 沿 X 方向移動 5,可用的方法首先將其移動到 (0,0),然后再移動到 (5,0); 但我需要將它從實際的 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);

只需使用QRect::translate 在您的特定情況下,這將類似於...

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

或者,如果您想不修改原始QRect ...

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM