Important: Please read the Qt Code of Conduct -

QGraphicsView resizing

  • Hello people,

    I created a custom class, which inherits QGraphicsView to set a background (which I draw myself in another class that inherits from QGraphicsScene, because QGraphicsView::setBackroundBrush() draws the image as a pattern). I want the QGraphicsView to be the exact size of the image I'm passing to the setter, so I did something like this:

    void GridView::setBackgroundImage(const QImage &image)

    However, the image gets cropped from the right side and the bottom. I currently draw the image like this:

    void GridScene::drawBackground(QPainter *painter, const QRectF &rect)
        painter->drawImage(sceneRect(), m_backgroundImage, m_backgroundImage.rect());

    but I used different parameters as well, with no luck.

    It may be important to mention that I call the following method on the view, so that I have a coordinate system which starts at the top left corner:

    setAlignment(Qt::AlignTop | Qt::AlignLeft);

    Am I missing something about the Graphics View Coordinate System or is it something else I'm doing wrong?
    I'm also open for different approaches to prevent the image from scaling to a wrong ratio. Any help is appreciated!


  • Lifetime Qt Champion

    QRect has feature:

    Note that for historical reasons the values returned by the bottom() and right() functions deviate from the true bottom-right corner of the rectangle: The right() function returns left() + width() - 1 and the bottom() function returns top() + height() - 1. The same is the case for the point returned by the bottomRight() convenience function. In addition, the x and y coordinate of the topRight() and bottomLeft() functions, respectively, contain the same deviation from the true right and bottom edges.

    so if u are missing like a pixel in right side, that could be it :)

  • Thanks for your answer! Unfortunately it's more than a pixel that I'm missing :/
    But you have a point anyway, so I might add those pixels to the scene rect.

  • Hi to all, i am stuck in qml, if someone knows how can resolve below issue will great to me?

    I have a tableview with three columns, inside third column i used button with image on every cell of the same, when user pressed it's image source change from red to green that is fine else red.

    The problem if i am updating my model on every 1 min so before updating it get to cleared and insert again in model successfully but during this all cell's button again goes red in view which was green, so please let me know how can resolve the same?

  • @Vicky-Sharma please start a new topic about your question, and do not post it to other topics that are not related.
    When you have your own topic people will find it and are then more likely to help.

  • ok will do