Results 1 to 4 of 4

Thread: Mouse events in QGraphicsPixmapItem and QGraphicsTextItem

  1. #1
    Join Date
    May 2007
    Posts
    106
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Mouse events in QGraphicsPixmapItem and QGraphicsTextItem

    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 :-(
    Qt Code:
    1. QRectF ITGraphicsPixmapItem::boundingRect() const
    2. {
    3. qreal penWidth = 1;
    4. QRectF rect = QRectF(0-penWidth/2,0-penWidth/2,itemSize.width()+penWidth,itemSize.height()+penWidth);
    5. if(itemIsSelected)
    6. rect.adjust(-m_CornerSize,-m_CornerSize,m_CornerSize,m_CornerSize);
    7.  
    8. return rect;
    9. }
    To copy to clipboard, switch view to plain text mode 
    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?

    Qt Code:
    1. QRectF ITGraphicsTextItem::boundingRect() const
    2. {
    3. qreal penWidth = 1;
    4. QRectF rect = QGraphicsTextItem::boundingRect();
    5.  
    6. if(itemIsSelected)
    7. rect.adjust(-m_CornerSize,-m_CornerSize,m_CornerSize,m_CornerSize);
    8.  
    9. return rect;
    10. }
    To copy to clipboard, switch view to plain text mode 
    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

  2. #2
    Join Date
    Jul 2008
    Posts
    27
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Mouse events in QGraphicsPixmapItem and QGraphicsTextItem

    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 .
    If God has friends ,then I cant be God.

  3. #3
    Join Date
    May 2007
    Posts
    106
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: Mouse events in QGraphicsPixmapItem and QGraphicsTextItem

    I reimplemented shape function both in PixmapItem and Text Item. Following is the code I written in both classes for shape function:

    Qt Code:
    1. QPainterPath ITGraphicsTextItem::shape() const
    2. {
    3. QPainterPath painterPath;
    4. painterPath.addRect(boundingRect());
    5. return painterPath;
    6. }
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. QPainterPath ITGraphicsPixmapItem::shape() const
    2. {
    3. QPainterPath painterPath;
    4. painterPath.addRect(boundingRect());
    5. return painterPath;
    6. }
    To copy to clipboard, switch view to plain text mode 

    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()!!

  4. #4
    Join Date
    May 2007
    Posts
    106
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: Mouse events in QGraphicsPixmapItem and QGraphicsTextItem

    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:

    Qt Code:
    1. bool ITGraphicsTextItem::contains(const QPointF &point) const
    2. {
    3. return QGraphicsItem::contains(point);
    4. }
    To copy to clipboard, switch view to plain text mode 

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.