Results 1 to 9 of 9

Thread: Qt 4.5 bugs in graphics view framework?

  1. #1
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    366
    Thanks
    87
    Thanked 12 Times in 12 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Qt 4.5 bugs in graphics view framework?

    Hello,

    I upgraded from Qt 4.8 to 5.4 and I noticed odd behaviors of my gui application. I'm using the graphics view framework and my setup is the standard items in a scene in a graphics view layout. The first thing I noticed is that my QGraphicsRectItems are now suddenly huge compared to before. I use the setRect() method in the constructor to define the size like so:

    Qt Code:
    1. Foot::Foot(QGraphicsItem *parent)
    2. {
    3. setRect(-0.05, -0.035, 0.1, 0.07);
    4. }
    To copy to clipboard, switch view to plain text mode 

    It seems that now the setRect() methods add to the default size rather than replacing it, such that I have to subtract a unit rectangle to get the same size I intended before like so:

    Qt Code:
    1. Foot::Foot(QGraphicsItem *parent)
    2. {
    3. setRect(-0.05 +0.5, -0.035 +0.5, 0.1 -1, 0.07 -1);
    4. }
    To copy to clipboard, switch view to plain text mode 

    Is this intended behavior or a bug?


    The second thing I noticed is that the item discovery seems to be broken. The attached screenshot Screenshot from 2015-04-22 14:58:11.jpg was produced by the following code in my graphics view:

    Qt Code:
    1. class GraphicsViewWidget : public QGraphicsView {...}
    2.  
    3. void GraphicsViewWidget::drawForeground(QPainter* painter, const QRectF& rect)
    4. {
    5. painter->resetTransform(); // The painter draws in scene coordinates by default. We want draw in view coordinates.
    6.  
    7. // Item detector.
    8. QGraphicsScene* sc = scene();
    9. for (int i = 0; i < width(); i+=5)
    10. {
    11. for (int j = 0; j < width(); j+=5)
    12. {
    13. int itemCount = sc->items(mapToScene(i,j)).size();
    14. if (itemCount > 0)
    15. painter->drawEllipse(i, j, itemCount, itemCount);
    16. }
    17. }
    18. }
    To copy to clipboard, switch view to plain text mode 

    The above code draws a dot at the view coordinates where at least one item was found in the scene. All items in the scene are covered by dots, but also a lot of places where there are no items. When I use the QGraphicsView::items() method, the item discovery works perfectly. QGraphicsScene::items() seems to be broken, however, and it has implications to the UI. The dotted areas respond to the mouse and the scene / the items believe that the mouse is on the item, which is not true. To me this clearly looks like a bug, but in any case, what can I do to fix this?

    Cheers,
    Cruz

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qt 4.5 bugs in graphics view framework?

    Your first statement is definitely false, about the second one, I don't know I'd have to check but it'd be easier to do it with a minimal compilable example reproducing the problem

    In general your for loop is over complicated. It should be enough to iterate over all items in the scene and draw the dots there instead of checking each chunk of the scene whether there are items there. I also don't understand why you want to draw in view coordinates if all your coordinates are scene coordinates.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    366
    Thanks
    87
    Thanked 12 Times in 12 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt 4.5 bugs in graphics view framework?

    Here I created an very nearly minimal example. ItemTest.zip
    It shows both that I have to subtract a unit rectangle to get the size I want, and that the item discovery is fishy. It behaves differently for the rectangle and the circle shown on the gui. The drawForeground method of the view "pokes" into the scene and asks for items. Items are reported in wrong places for both the block and the circle. When you click on the gui, the dotted space around the circle reports a mouse click on the circle.

  4. #4
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    366
    Thanks
    87
    Thanked 12 Times in 12 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt 4.5 bugs in graphics view framework?

    For all who encounter the same problem, here is a quick fix that fixes at least the mouse handling. First of all, according to the documentation setRect() is supposed to set the enclosing rect of the ellipse, which is also used as boundingRect(). The following code:

    Qt Code:
    1. Circle::Circle(QGraphicsItem *parent)
    2. {
    3. comRadius = 0.04;
    4. setFlags(QGraphicsItem::ItemIsMovable);
    5. setRect(QRectF(-comRadius, -comRadius, 2.0*comRadius, 2.0*comRadius));
    6.  
    7. qDebug() << "rect:" << rect();
    8. qDebug() << "bounding rect:" << boundingRect();
    9. }
    To copy to clipboard, switch view to plain text mode 

    and its output

    rect: QRectF(-0.04,-0.04 0.08x0.08)
    bounding rect: QRectF(-0.54,-0.54 1.08x1.08)
    show that the bounding rect is not set up as intended. So I overwrote boundingRect().

    Qt Code:
    1. QRectF Circle::boundingRect()
    2. {
    3. return rect();
    4. }
    To copy to clipboard, switch view to plain text mode 


    And then in any mouse event I do this:

    Qt Code:
    1. void Circle::mousePressEvent(QGraphicsSceneMouseEvent *event)
    2. {
    3. if (!boundingRect().contains(event->pos()))
    4. return;
    5. QGraphicsItem::mousePressEvent(event);
    6. }
    To copy to clipboard, switch view to plain text mode 

    to discard mouse events that don't quite hit the item. It doesn't fix the item discovery, as QGraphicsScene::items() still reports items in the wrong place, but at least the mouse handling works again.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qt 4.5 bugs in graphics view framework?

    This is the implementation of bounding rect for the ellipse item:

    Qt Code:
    1. QRectF QGraphicsEllipseItem::boundingRect() const
    2. {
    3. if (d->boundingRect.isNull()) {
    4. qreal pw = pen().style() == Qt::NoPen ? qreal(0) : pen().widthF();
    5. if (pw == 0.0 && d->spanAngle == 360 * 16)
    6. d->boundingRect = d->rect;
    7. else
    8. d->boundingRect = shape().controlPointRect();
    9. }
    10. return d->boundingRect;
    11. }
    To copy to clipboard, switch view to plain text mode 

    You can see that it returns the rectangle if there is no pen (or there is cosmetic (width=0.0) pen set), otherwise it takes the pen width into consideration so that you don't get artifacts with non-consmetic pen sizes.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    366
    Thanks
    87
    Thanked 12 Times in 12 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt 4.5 bugs in graphics view framework?

    I don't know what this is supposed to show me. The minimal example I compiled on your request shows that using the setRect() method in the constructor does not set the bounding rect right, and that there are item discovery issues in the scene.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qt 4.5 bugs in graphics view framework?

    It does set the bounding rect right. The bounding rect is the rectangle extended by half the pen width. If you set the pen of your item to Qt::NoPen, you will notice that bounding rect will be the same as the rect you set.

    And what exactly is wrong with collision detection? For me it looks like it behaves correctly.
    Last edited by wysota; 25th April 2015 at 16:44.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    366
    Thanks
    87
    Thanked 12 Times in 12 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt 4.5 bugs in graphics view framework?

    Quote Originally Posted by wysota View Post
    If you set the pen of your item to Qt::NoPen, you will notice that bounding rect will be the same as the rect you set.
    That's it! So it turns out that there is a default pen set with width 1 which then messes up the bounding boxes because the sizes in my scene are much smaller than 1. Setting NoPen fixes both issues, which are then essentially the same.

    So now I wonder why it has worked with Qt 4.8. Has the default pen size of 1 been introduced in version 5?

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qt 4.5 bugs in graphics view framework?

    Quote Originally Posted by Cruz View Post
    That's it! So it turns out that there is a default pen set with width 1 which then messes up the bounding boxes because the sizes in my scene are much smaller than 1. Setting NoPen fixes both issues, which are then essentially the same.

    So now I wonder why it has worked with Qt 4.8. Has the default pen size of 1 been introduced in version 5?
    In Qt 4.8 apparently boundingRect() of QGraphicsRectItem was broken not to take pen width into consideration. The default pen width for QGraphicsRectItem has always been 1.

    I am wondering though why you are using QGraphicsRectItem at all if you are not using any of its features. If you derived from QGraphicsItem instead, you wouldn't have had all these problems.

    By the way, often it really pays of to have Qt source code at hand and actually check what the method you are using does.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Problem about Graphics View Framework
    By FinderCheng in forum Qt Programming
    Replies: 5
    Last Post: 3rd November 2009, 09:16
  2. Problem with Graphics View Framework
    By Disperato in forum Qt Programming
    Replies: 1
    Last Post: 27th May 2009, 21:44
  3. Issues with the Graphics View Framework
    By lni in forum Qt Programming
    Replies: 8
    Last Post: 26th April 2009, 15:13
  4. about the contextMenuEvent in graphics view framework
    By bingoking in forum Qt Programming
    Replies: 1
    Last Post: 21st April 2009, 07:04
  5. Replies: 4
    Last Post: 5th August 2008, 20:55

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.