我使用在場景事件處理程序中更改QGraphicsItems的位置?
void QGraphicsItem::installSceneEventFilter(QGraphicsItem * filterItem);
設置上的QGraphicsItem事件過濾器(見itemChanged() in QGraphicsItem for a many different items)現在
,其中一些項目,我想限制的運動,即改變x和y項的位置,以便用戶在物體移動的某個區域受到限制。
我第一次嘗試與修改的事件:
(static cast <QGraphicsSceneMouseEvent*>(event))->setPos(QPoint(150, watched->y()));
整個處理程序17930:
bool generic_graphic_item::sceneEventFilter(QGraphicsItem* watched, QEvent* event)
{
if(event->type() == QEvent::QEvent::GraphicsSceneMouseMove)
{
(static_cast<QGraphicsSceneMouseEvent*>(event))->setPos(QPointF(150, watched->y()));
//emit my_item_changed(watched); // signal that the item was moved
emit(item_pos_changed(watched, watched->x(), watched->y()));
}
return false; // pass the event to the original target item
}
但沒有奏效。我不確定隱藏在QEvent::GraphicsSceneMouseEvent
背後的具體事件類別。
然後我試着撥打事件處理程序中watched->setX()
和watched->setY()
,但不是很受歡迎... ...這我也能理解......
是否有可能限制現場事件處理程序內的運動?
我已閱讀,QGraphicsItem::itemChange()
可用來做到這一點,但後來我回在「itemChanged() in QGraphicsItem for a many different items」描述的問題,即我怎麼能有這種共同的許多項目沒有繼承他們每個人的...
非常感謝,
感謝Merlin069抽出時間來回答。 如果我按照你的建議使用QEvent :: GraphicsSceneMove,那麼我甚至不會用新的位置得到信號item_pos_changed。我的代碼適用於此,即item_pos_changed信號被命中。 但我不知道如何將事件更改爲我想要的鼠標位置,在上面的示例中,只有alowing沿x = 150的水平線移動 – user1159290