PDA

View Full Version : unified the cursor and item coordinate



ensky_cy
7th December 2009, 07:36
int i = 1;
QPoint posi = QCursor::pos();
QPointF postemp = (QPointF)posi;
foreach(coordinator cor1, m_pageListback1)
{
if((cor1.x <= postemp.x()) && (cor1.y <= postemp.y()) && ((cor1.x+192) >= postemp.x()) && ((cor1.y+256) >= postemp.y()))
{
pagenum = i; //index of the item in the QVector
break;
}
i++;
}

m_pageListback1 is a QVector Container ,they are items' top-left coordinate(these coordinates are the result of QGraphicsItem::mapToScene ).every item's width = 192,height = 256.I want to judge ,whether the cursor is in the scope of the item.
but it is not accurate.Is there some way to unify the coordinate.
Thanks!!!!!

spirit
7th December 2009, 07:46
you can get item's rect and test if a point contains in the rect using QRect::contains.

ensky_cy
7th December 2009, 08:06
Thanks!
Do you mean:


int i = 1;
QPoint posi = QCursor::pos();
QPointF postemp = (QPointF)posi;

foreach(QGraphicsItem* page, m_pageList)
{
if(page->contains(postemp))
{
pagenum = i;
break;
}
i++;
}

I try this,but it doesn't work.

spirit
7th December 2009, 08:08
try this


...
if(page->boundingRect().contains(postemp))
...

ensky_cy
7th December 2009, 08:41
Thank you!
Sorry,I didn't say it clear.
1.the page item , I haven't reimplemented boundingRect();
2.the page items, I have reduced these items by 20%,then relocated them,let them in a sequence.