我在场景中有多个QGraphicsItem,分布在场景的不同部分。在应用中,有不同的模式之一,用户可以滚动场景(掌上拖动模式)。实现对场景I的滚动,将dragMode QGraphicsView 的设置为 ScrollHandDrag.
但问题是,当用户试图通过拖动(MousePress和MouseMove)在任何QGraphicsItem上滚动场景而不是滚动场景时,它会移动QGraphicsItem。
我怎样才能停止QGraphicsItem 的移动,滚动场景,但我仍然想选择QGraphicsItem?
任何解决方案或任何指针都会有所帮助。
注意:有很大数量的QGraphicsItem,并且是各种类型的。因此,不可能在QGraphicsItem上安装事件过滤器。
发布于 2013-04-22 15:07:38
我没有修改项目标志,而是在ScrollHandDrag模式下设置了整个视图,而不是交互式视图。问题是,您需要一个额外的交互类型(即控制键、其他鼠标按钮等)来启用它。
setDragMode(ScrollHandDrag);
setInteractive(false);发布于 2012-10-17 13:22:44
解决了!!
请参考我在Qt论坛上提出的问题:单击此处
解决方案/实例:
void YourQGraphicsView::mousePressEvent( QMouseEvent* aEvent )
{
if ( aEvent->modifiers() == Qt::CTRL ) // or scroll hand drag mode has been set - whatever condition you like :)
{
QGraphicsItem* pItemUnderMouse = itemAt( aEvent->pos() );
if ( pItemUnderMouse )
{
// Track which of these two flags where enabled.
bool bHadMovableFlagSet = false;
bool bHadSelectableFlagSet = false;
if ( pItemUnderMouse->flags() & QGraphicsItem::ItemIsMovable )
{
bHadMovableFlagSet = true;
pItemUnderMouse->setFlag( QGraphicsItem::ItemIsMovable, false );
}
if ( pItemUnderMouse->flags() & QGraphicsItem::ItemIsSelectable )
{
bHadSelectableFlagSet = true;
pItemUnderMouse->setFlag( QGraphicsItem::ItemIsSelectable, false );
}
// Call the base - the objects can't be selected or moved by this click because the flags have been un-set.
QGraphicsView::mousePressEvent( aEvent );
// Restore the flags.
if ( bHadMovableFlagSet )
{
pItemUnderMouse->setFlag( QGraphicsItem::ItemIsMovable, true );
}
if ( bHadSelectableFlagSet )
{
pItemUnderMouse->setFlag( QGraphicsItem::ItemIsSelectable, true );
}
return;
}
}
// --- I think This is not required here
// --- as this will move and selects the item which we are trying to avoid.
//QGraphicsView::mousePressEvent( aEvent );
}https://stackoverflow.com/questions/12928190
复制相似问题