我已经创建了一个QGraphicsScene场景,并在场景中添加了一些图形项目(行、矩形)等。
我可以使用以下列表遍历它们:
QList<QGraphicsItem*> all = items();我启用了这些项目的移动,我能够通过单击选择它们来拖动它们。但是,在一个元素被拖动之后,它将停止出现在对items()函数的QGraphicsScene调用中。
QList<QGraphicsItem*> all = items();拖动的项目没有出现在上面的列表中,而非拖动的项目却会出现。
拖动QGraphicScene元素会改变其父元素吗?或者其他人能为这样的问题提出建议的原因?
{P.S.代码太大,无法共享}
编辑1:
我使用标志QGraphicsItem::ItemIsSelectable和QGraphicsItem::ItemIsMovable使项目可移动。
foreach(QGraphicsItem* itemInVisualScene, items())
{
itemInVisualScene->setFlag(QGraphicsItem::ItemIsSelectable, itemsMovable);
itemInVisualScene->setFlag(QGraphicsItem::ItemIsMovable, itemsMovable);
}默认情况下,我在场景中添加了几个矩形。然后,在“移动模式”中,我会把它们拖来拖去。然后,在“添加模式”中,我单击屏幕添加新的矩形。我编写了一个逻辑来检查是否正在单击任何现有的绘制矩形:
void Scene::mousePressEvent(QGraphicsSceneMouseEvent * event)
{
if(eDrawLines == sceneMode)
{
dragBeginPoint = event->scenePos();
dragEndPoint = dragBeginPoint;
QList<QGraphicsItem*> all = items();
for (int i = 0; i < all.size(); i++)
{
QGraphicsItem *gi = all[i];
// Clicked point lies inside existing rect
if( QGraphicsRectItem::Type == gi->type() && gi->contains(dragBeginPoint))
{
std::cout << "Pressed inside existing rect" << std::endl;
return;
}
}
std::cout << "Point not found, add new rectangle" << std::endl;
}
QGraphicsScene::mousePressEvent(event);
}对于没有在“移动模式”中拖动的rects,这个矩形的加法很好。但是被移动的rects似乎不再识别点击。即使当我单击先前拖动的现有矩形时,我的控件也会从循环中退出。
发布于 2015-07-06 14:39:25
拖放后会更改QGraphicsItem的转换,因此需要将点转换为项的本地坐标。
gi->contains(gi->mapFromScene(dragBeginPoint))若要转换或获取项目在场景坐标中的位置,请使用
gi->mapToScene(0,0) or gi->scenePos()https://stackoverflow.com/questions/31243913
复制相似问题