PDA

View Full Version : The function QGraphicsScene::itemAt problem



yaseminyilmaz
2nd April 2012, 10:07
Hi all,

I've created a custom class (named as Flow) inherited by QGraphicsItem. It's composing of an arrow and curve. I've used the function itemAt to determine which item has selected when QGraphicsScene::mousePressEvent is called. But it does not always return correct item. Otherwise I've written the folllowing function for manual detection. It's working correctly. But I want to learn why sometimes QGraphicsScene can not detect item selected by pressing mouse? Is there a more sensible method than manual point control?



int getMatchedFlow(QPointF pos)
{
int fID = -1;
QList<Flow*> mFlowList = mGroupStore.keys();
QList<Flow*>::iterator i;
Flow *pFlow;

for (i = mFlowList.begin(); i != mFlowList.end(); ++i)
{
pFlow = (Flow*)*i;
if (pFlow->checkPointSet(pos))
{
fID = pFlow->getID();
break;
}
}

return fID;
}

bool Flow::checkPointSet(QPointF point)
{
bool rc = false;

if (mBoundingPath.contains(point))
{
rc = true;
}

return rc;
}


Thanks a lot for your helps,

wysota
2nd April 2012, 11:50
How did you implement boundingRect() and shape() for your item?

yaseminyilmaz
2nd April 2012, 13:16
I've implemented as following. I've noticed that sometimes getMatchedFlow has not detect when I click the arrow in addition to the function QGraphicsScene::itemAt. What can the problem be?



QPainterPath Flow::shape() const
{
return mBoundingPath;
}

QRectF Flow::boundingRect() const
{
return mRectF;
}

void Flow::updateBoundingRect()
{
prepareGeometryChange();

mBoundingPath = QPainterPath();

addArrow(); // it adds arrow polygon to mBoundingPath
addCurve(); // it adds curve path to mBoundingPath

mRectF = mBoundingPath.boundingRect();

updateScene();
}

wysota
2nd April 2012, 13:48
Do you have overlapping items? If so, do you have different zorder set for them?