2013-11-24 91 views
0

我使用在场景事件处理程序中更改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”描述的问题,即我怎么能有这种共同的许多项目没有继承他们每个人的...

非常感谢,

+0

感谢Merlin069抽出时间来回答。 如果我按照你的建议使用QEvent :: GraphicsSceneMove,那么我甚至不会用新的位置得到信号item_pos_changed。我的代码适用于此,即item_pos_changed信号被命中。 但我不知道如何将事件更改为我想要的鼠标位置,在上面的示例中,只有alowing沿x = 150的水平线移动 – user1159290

回答

0

您在这个问题中发布的代码是响应鼠标移动的事件。对于你描述你想要做什么,我建议你检查小部件的情况下就动了的QEvent :: GraphicsSceneMove: -

if(event->type() == QEvent::GraphicsSceneMove) 
{ 
    // set the position of the item. 
} 
相关问题