Results 1 to 7 of 7

Thread: boundingRect()?

  1. #1
    Join Date
    Mar 2009
    Posts
    20
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default boundingRect()?

    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 !
    Attached Images Attached Images

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: boundingRect()?

    You have to deal with GraphicsItem::shape().

  3. #3
    Join Date
    Mar 2009
    Posts
    20
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: boundingRect()?

    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 !

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: boundingRect()?

    Qt Code:
    1. QPainterPath shape() const
    2. {
    3. //mPolygn is a QPolygonF type, and is set three points which are start and end point of line.
    4. path.addPolygn(mPolygn);
    5. return path;
    6. }
    To copy to clipboard, switch view to plain text mode 
    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.

  5. #5
    Join Date
    Mar 2009
    Posts
    20
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: boundingRect()?

    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..

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: boundingRect()?

    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

  7. #7
    Join Date
    Mar 2009
    Posts
    20
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: boundingRect()?

    Hi Lykurg,
    Ok, thanks, I will try again.

Similar Threads

  1. How to realiably create the boundingRect()
    By zarkzervo in forum Qt Programming
    Replies: 1
    Last Post: 19th February 2009, 13:00
  2. setRect vs boundingRect
    By zgulser in forum Qt Programming
    Replies: 5
    Last Post: 5th February 2009, 00:23
  3. Replies: 1
    Last Post: 31st October 2008, 08:26
  4. QGraphicsItem force boundingRect
    By bunjee in forum Qt Programming
    Replies: 0
    Last Post: 27th September 2008, 16:49
  5. How to avoid calling boundingRect() & shape()
    By nileshsince1980 in forum Qt Programming
    Replies: 7
    Last Post: 8th October 2007, 18:10

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.