Important: Please read the Qt Code of Conduct -

QGraphicsView doesnt render properly?

  • I'm using the graphics framework (QGraphicsView, QGraphicsScene, QGraphicsEllipseItem, QGraphicsRectItem) to draw ellipses and rectangles in an area using the mouse (press, move, release) events. Just like happen when we use the MSPaint.

    To get mouse events, I created an class (GraphicsScene) that inherites of QGraphicsScene. When the user select and item to draw, lets say an ellipse, and click on draw area, I catch that first point (using mousepress event). When the user move the mouse I catch a second point, do some calculus to get the width and height of that rectangle, and use all those information in setRect method of QGraphicsEllipseItem.

    The following code, does that:

    @void GraphicsScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
    ellipse->setRect(ellipse->rect().x(), ellipse->rect().y(),
    event->scenePos().x() - ellipse->rect().x(),
    event->scenePos().y() - ellipse->rect().y());

    Everything works fine with ellipse. The problem is when I use the same idea to draw and rectangle.

    When I draw a rectangle, from up-left to down-right, it works fine, but if I tray any other direction for example from down to up (no matter if is right or left) the rectangle drawed doesnt appear (look Image1), just appear an line! But if in the next I draw something on top of it, that rectangle that was only a line, appears normally (look Image2).

    Image 1
    ! 1)!

    Image 2
    ! 2)!

    I tried to draw the rectangle with a lot of other ways like: setting the top-left and bottom-right points, setting all corner points, and so on... but nothing works!

    I'm thinking that probably is a Qt's bug, because the same idea works in a ellipse, but not with rectangles!

    Please guys, what am I doing wrong?

    I'm using Qt 5 on Kubuntu, but have tried on latest Qt's version on Win7 and the problem is the same.

  • Lifetime Qt Champion


    You could be indeed hitting a bug, did you try to look at the "bug report system": to see if it's something known ? If not you could open a new report with a minimal compilable example to reproduce this

  • Hi.

    Yes, I searched on bug report system, but I didnt found anything related to that.

    I'll follow your advice, thank you.

  • Hi Helder

    I would try to draw a valid rectangle instead. It should have positive height and width.

  • Hi soroush, thank you to reply.

    I've tried draw a valid rectangle, but without success. Probably because there is something wrong in my code.

    But as I said before, if I use the same idea to draw an ellipses, it works fine even with an invalid rectangle!

Log in to reply