我使用在场景事件处理程序中更改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