[Solved] QGraphicsScene/View displays empty rect



  • Hi,

    If I add a rect to a QGraphicsScene with a zero height, the rect is still displayed on the QGraphicsView.

    !http://i42.tinypic.com/2pt8dg5.jpg!

    Created with:

    @QMatrix matrix;
    matrix.scale(8, -8);
    graphicsView.setMatrix(matrix);

    scene.addRect(0, 0, 10, 5, QPen(QColor("yellow"), 1));
    scene.addRect(0, 0, 1, 0, QPen(QColor("red"), 1));
    scene.addRect(5, 0, 5, 0, QPen(QColor("blue"), 0), QBrush(QColor("green"), Qt::NoBrush));
    @

    The complete source file is here: "Empty Rect Example":http://pastebin.com/64tkStUe

    What can I do to make this (red and blue) rect to not display anything but still be there?



  • The pen specifies the border of the rectangle. Basically, a rectangle of height zero behaves like a line, so it's logical that something will be painted.

    1. Use a Brush instead of a Pen to fill the interior of the rectangle instead of painting the border. A rectangle with height zero should have no interior, and therefore not draw
    2. You can always write your own rectangle item that behaves exactly as you want...


  • Great!

    I've changed line 5,6 and 7 to:

    @
    scene.addRect(0, 0, 10, 5, QPen(QBrush(Qt::NoBrush), 0), QBrush(QColor("yellow")));
    scene.addRect(0, 0, 1, 0, QPen(QBrush(Qt::NoBrush), 0), QBrush(QColor("#red")));
    scene.addRect(5, 0, 5, 0, QPen(QBrush(Qt::NoBrush), 0), QBrush(QColor("#blue")));
    @



  • I think you can simplify to

    @scene.addRect(0, 0, 10, 5, QPen(Qt::NoPen), QBrush(QColor("yellow")));@



  • Cool! Tx.

    And of course '#red' should be just 'red'.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.