PDA

View Full Version : Mouse events in QGraphicsPixmapItem and QGraphicsTextItem



manojmka
4th August 2008, 15:07
Hi,

I have problems getting mouse events on my custom implemented pixmap item and text item. I am implementing boundingRect function in following way, can you please suggest what am I doing wrong? It seems that I am making some obvious mistake but not able to catch it :-(


QRectF ITGraphicsPixmapItem::boundingRect() const
{
qreal penWidth = 1;
QRectF rect = QRectF(0-penWidth/2,0-penWidth/2,itemSize.width()+penWidth,itemSize.height()+penW idth);
if(itemIsSelected)
rect.adjust(-m_CornerSize,-m_CornerSize,m_CornerSize,m_CornerSize);

return rect;
}

This way I get hover even only if the mouse cursor in somewhere boundary like (0,0,itemSize.width()+m_CormerSize,itemSize.height ()+m_CornerSize). It seems to be some problem with negative x and y coordinates. What is the correct way to implement this then?


QRectF ITGraphicsTextItem::boundingRect() const
{
qreal penWidth = 1;
QRectF rect = QGraphicsTextItem::boundingRect();

if(itemIsSelected)
rect.adjust(-m_CornerSize,-m_CornerSize,m_CornerSize,m_CornerSize);

return rect;
}


This way I get hover event only if the mouse cursor in QGraphicsTextItem::boundingRect(). While painting, I can easily use bounding Rect with intended results but mouse events don't work on updated bounding rect. Can you please help me, Why is it doing so?

Regards,
Manoj

Benne Gesserit
4th August 2008, 21:24
I am not sure,but I think item events occur on a shape ,not on a bounding rect.boundingRect() may not be enough accurate to tell if the mouse hovers the item .If I am right,I would check shape functions .

manojmka
5th August 2008, 12:13
I reimplemented shape function both in PixmapItem and Text Item. Following is the code I written in both classes for shape function:



QPainterPath ITGraphicsTextItem::shape() const
{
QPainterPath painterPath;
painterPath.addRect(boundingRect());
return painterPath;
}





QPainterPath ITGraphicsPixmapItem::shape() const
{
QPainterPath painterPath;
painterPath.addRect(boundingRect());
return painterPath;
}


To my surprise it works perfectly for Pixmap Item but fur text item it only works with Mouse Hover events. Mouse click is still grabbed on only QGraphicsTextItem::boundingRect()!!

manojmka
8th August 2008, 12:38
I finally found the solution to this problem. I needed to also over ride contains() function in my textItem class. Qt is by default using some boundingRect data member instead of boundingRect() function in this contains() implementation fo rQGraphicsTextItem. I did it the following way:



bool ITGraphicsTextItem::contains(const QPointF &point) const
{
return QGraphicsItem::contains(point);
}