PDA

View Full Version : boundingRect()?



aaron
20th April 2009, 09:33
Dear all,
In my program, I draw a line via two points, the line's parent class is QGraphicsItem.
I want to select a line when it is under mouse, but the boundingRect is not my expection.
The real boundingRect likes the rectangel(red line) in figure_1(left) , otherwise, I want to get the boundingRect likes figure_2(right). If I uses boundingRect likes figure_1, when I move mouse to the rectangle, the line(cyan line) will be selected, but, it is not under mouse currsor.
According to the Qt4's manual, I re-implement the two pure virsual function: boundingRect() && paint(...), if I do not use boundingRect(), How can I do it?
Thanks a lot !

Lykurg
20th April 2009, 09:41
You have to deal with GraphicsItem::shape().

aaron
20th April 2009, 15:57
Hi Lykurg ,
Thanks for your help.
I re-implement the QGraphicsItem::shape(), like below:
QPainterPath shape() const
{
QPainterPath path = QGraphicsItem::shape();
path.addRect(boundingRect());
return path;
}
and I try the below code too,but they have no effect.
QPainterPath shape() const
{
QPainterPath path = QGraphicsItem::shape();
//mPolygn is a QPolygonF type, and is set three points which are start and end point of line.
path.addPolygn(mPolygn);
return path;
}
Where is my wrong and how to fix it? Thanks !

Lykurg
20th April 2009, 19:22
QPainterPath shape() const
{
QPainterPath path;
//mPolygn is a QPolygonF type, and is set three points which are start and end point of line.
path.addPolygn(mPolygn);
return path;
}
This one seems fine (Skip the "QGraphicsItem::shape()"). How do you detect if the mouse is over the item. Can you show the code? May be there is the error.

aaron
21st April 2009, 09:39
Hi Lykurg,
I get the items like below when mouse over them.

//MyGraphicsView inherites QGraphicsView class
//MyGraphicsItem inherites QGraphicsItem class

void MyGraphicsView::mouseMoveEvent(QMouseEvent *e)
{
QPointF pf = mapToscene(e->pos);
MyGraphicsIte *item = getPointedItem(pf.toPoint());
setItemFlags(item);
}

MyGraphicsItem* MyGraphicsView::getPointedItem(QPoint point) const
{
QList<QGraphicsItem *> list = scene()->items(point);
QList<QGraphicsItem *>::iterator itr = list.begin();
for(; itr != list.end(); itr++)
{
if((*itr) != NULL)
{
MyGraphicsItem *item = dynamic_cast<MyGraphicsItem *>(*itr);
return item;
}
}
return NULL;
}

void MyGraphicsView::setItemFlags(MyGraphicsItem *item)
{
if(item != NULL)
{
item->mbActive = true;
.....
}
}

When drawing the line, if the mbActive is true , the line's color will be highlight.

Maybe , here is wrong ,but I can not find..:(

Lykurg
21st April 2009, 09:54
Hi,

some thoughts:

Use the mouseMoveEvent handler direct in your MyGraphicsItem
There test if the current pos is on the line, set mbActive and repaint.

In your code you probably forget to repaint the item. (Beside the code is sometimes strange! Don't return NULL, and the for scope in getPointedItem is not necessary. And checking each mouse move event in your view is very heavy and also not necessary if you put the code in your items.)

Lykurg

aaron
21st April 2009, 10:43
Hi Lykurg,
Ok, thanks, I will try again.