Results 1 to 5 of 5

Thread: Scaling the image in GraphicsView and Scene is cutting the right border of image

  1. #1
    Join Date
    May 2013
    Posts
    3
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Scaling the image in GraphicsView and Scene is cutting the right border of image

    Hi,

    I’m trying to scale a image to fit in the QGraphicView. Sometimes it works, but for example, if I load a image of size 1500×900, the resized image is cut a bit in the right border, even with the scrool activated. What is wrong in the code below?

    Qt Code:
    1. int sceneWidth = 800;
    2. int sceneHeight = 600;
    3.  
    4. ui->graphicsView->setFixedWidth(sceneWidth);
    5. ui->graphicsView->setFixedHeight(sceneHeight);
    6.  
    7. // mScene is a subclass of QGraphicsScene
    8. mScene->clear();
    9.  
    10. if (!imageQt.isNull()) { // imageQt is QImage
    11.  
    12. QPixmap originalImage = QPixmap::fromImage(imageQt);
    13. QPixmap scaledImage = QPixmap(originalImage.scaledToHeight( (int) mScene->height(), Qt::SmoothTransformation) );
    14.  
    15. ui->graphicsView->setFixedWidth( scaledImage.width() );
    16. ui->graphicsView->setFixedHeight( scaledImage.height() );
    17.  
    18. mScene->addPixmap(scaledImage);
    19. ui->graphicsView->setScene(mScene);
    20. ui->graphicsView->setSceneRect(scaledImage.rect());
    21. ui->graphicsView->fitInView(mScene->itemsBoundingRect() , Qt::KeepAspectRatio);
    22.  
    23. ui->graphicsView->show();
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Scaling the image in GraphicsView and Scene is cutting the right border of image

    I’m trying to scale a image to fit in the QGraphicView.
    Then you should scale to QGraphicsView's hieght.
    Qt Code:
    1. //QPixmap scaledImage = QPixmap(originalImage.scaledToHeight( (int) mScene->height(), Qt::SmoothTransformation) );
    2. QPixmap scaledImage = originalImage.scaledToHeight(ui->graphicsView->height(), Qt::SmoothTransformation);
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. The following user says thank you to Santosh Reddy for this useful post:

    icedecker (15th May 2013)

  4. #3
    Join Date
    May 2013
    Posts
    3
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Scaling the image in GraphicsView and Scene is cutting the right border of image

    Thanks, I have tried it now, but not worked

  5. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Scaling the image in GraphicsView and Scene is cutting the right border of image

    Thanks, I have tried it now, but not worked
    What is the output like?
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  6. #5
    Join Date
    May 2013
    Posts
    3
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Scaling the image in GraphicsView and Scene is cutting the right border of image

    Now it is working! I have set the width too small for images with big width. That is why the right border of the image was cut.

    So I first calculate the ratio of width and height, and scale on the height or the width, depending of the condition.

    Thanks for the help, the scaling on the graphicsview and not on the scene was useful.

  7. The following user says thank you to icedecker for this useful post:


Similar Threads

  1. Scaling window with image
    By danioto in forum Newbie
    Replies: 6
    Last Post: 20th October 2012, 14:58
  2. Non-scaling image added to scene
    By Thomas_Lerman in forum Qt Programming
    Replies: 10
    Last Post: 1st February 2011, 22:35
  3. QWebView and image scaling
    By naresh in forum Qt Programming
    Replies: 1
    Last Post: 25th November 2010, 04:38
  4. Border cutting with QGraphicsSvgItem
    By akiross in forum Qt Programming
    Replies: 2
    Last Post: 15th May 2007, 10:12
  5. Fast image drawing/scaling in Qt 3.3
    By eriwik in forum Qt Programming
    Replies: 1
    Last Post: 21st June 2006, 11:45

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.