Fit images of different size into my QGraphicsScene

  • Hello,

    I have a QGraphicsView of standard size 214x256. I have also created a QGraphicScene of the same size (214x256). But when I load an image having different size from 214x256 for example 94x128, it cannot be fit into my QGraphicsScene.How could I fit images of different size into my QGraphicsScene?


  • Moderators

    You could use "scale of QImage":

  • I have used a QGraphicsPixmapItem as seems from the following code. How could I scale me pixmapItem to feet to my QGraphicsScene (named scene)? The size of my scene should be equal to the size of the loaded image?

    @if (!pixmapItem) {
    pixmapItem = scene->addPixmap(QPixmap(fileName, 0, Qt::AutoColor));
    } else {
    pixmapItem->setPixmap(QPixmap(fileName, 0, Qt::AutoColor));

  • Moderators

    Also QPixmap has the "scale method":

    So you could load the pixmap to a temporary pixmap and then rescale with the values you have.

    QPixmap tmpmap (fileName, 0, Qt::AutoColor);
    pixmapItem = scene->addPixmap ( tmpmap.scaled ( width, height ) );
    Similar you have to do in the else case.

  • I have wrote this to my code,but still I cannot fit my png image to the size of my QGraphicsView. size_x and size_y are the xy values of my loaded image

    @ static QGraphicsPixmapItem* pixmapItem;

    if (!pixmapItem) {

    QPixmap tmpmap (QPixmap("2D.png"));
        pixmapItem = scene->addPixmap ( tmpmap.scaled ( size_x, size_y ) );
        } else {

    aslo to create scene I have subclassed QGraphicsScene, if it could help you:

    @Canvas::Canvas(): QGraphicsScene(0,0,214,256)

  • Moderators

    [quote author="jk_mk" date="1306595852"]I have wrote this to my code,but still I cannot fit my png image to the size of my QGraphicsView. size_x and size_y are the xy values of my loaded image

    Well, if tmpmap should have already the size of the loaded picture. Whatelse should it know. In the scaled method you should provide the target size (214, 256). Otherwise scaled will just return a copy.

  • Yes, you have right. But, I have one more question and I hope you could help me. Every time, I scale in an image what exactly I am doing concerning my data? To be more specific, do I change image's raw data? Because, now I had to display an image of 137x167, and I have scaled it into an image of 214x256. Is it harmful for the image quality? If yes, what should be done in order to maintain image quality (if it is possible)?

    Thanks for your help and your replies

  • the scaled method has the "transformation mode": argument.

    Qt::FastTransformation: The transformation is performed quickly, with no smoothing.
    Qt::SmoothTransformation: The resulting image is transformed using bilinear filtering.

    The second option results in a smoother image.

  • Moderators

    As long as you do not save the image to its initial source file you may do several scalings. E.g. you start your application with different sizes of your graphic scene and you always read it from original file, you should not face problems. The quality of the displayed may change with the actual size, but a new start from original file will have different scaling and a different result. See also the input of Jota for scaling quality. Consequence, you should not destroy your original file.

    If you load your image once and you do several scalings with different sizes, you will certainly have deterioration of the image.

Log in to reply

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