Nominate our 2022 Qt Champions!

How to align to the a rectangle to the left of a QGraphicsView without scaling the scene?

  • Here is what I wat to do.

    I need to draw in a QGraphicsView a series of rectangles that are aligned left and right. By this I mean that if rectangle i has posion (0,y) rectangle i+1 needs to have position (0,max) where max is such that the right side of the rectangle "touches" the right side of the QGraphicsView.

    When the window is resized I need to recalculate the value of max such that the rectangle is always touching the right side of the screen.

    Here is how I add my scene (this references a class that inherits QGraphicsView)

    scene = new QGraphicsScene(this);

    To add a rectangle that touches the left border I add it a (0,yvalue,width,height).

    How do I calculate the value of X so that that the rectangle will touch the right border?

  • Lifetime Qt Champion


    Are you thinking about something like x = scene->width() - rect->width() ?

Log in to reply