Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED]How to fit in view the pixmaps in QGraphicsView/QGraphicsScene without changing aspect ratio
QtWS25 Last Chance

[SOLVED]How to fit in view the pixmaps in QGraphicsView/QGraphicsScene without changing aspect ratio

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 13.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    adnan
    wrote on last edited by
    #1

    I am using QGraphicsView/QGraphicsScene to display an image. The image is always displayed to its original size with scroll bars at the ends. I want QGraphicsView to fit the image automatically as per the the size of the window keeping aspect ratio.
    I tried this but nothing happened:
    @ui->graphicsView->fitInView(0,0,ui->graphicsView->width(),ui->graphicsView->height(),Qt::KeepAspectRatio);@

    1 Reply Last reply
    0
    • M Offline
      M Offline
      msx_br
      wrote on last edited by
      #2

      The QGraphicsView scales the view matrix and scrolls the scroll bars to ensure that the scene rectangle rect fits inside the viewport. rect must be inside the scene rect; otherwise, fitInView() cannot guarantee that the whole rect is visible.

      You can try :
      @
      ui->graphicsView->ensureVisible ( scene->sceneRect() );
      ui->graphicsView->fitInView( scene->sceneRect(), Qt::KeepAspectRatio);
      @

      msx_br - Brazil (Netherlands)

      1 Reply Last reply
      0
      • A Offline
        A Offline
        adnan
        wrote on last edited by
        #3

        O thanks! but it works only when screen is maximized, if i change the window size the scroll bars appear again. Perhaps, i have to use this in resize event. We can use this also
        @ui->graphicsView->fitInView(scene->itemsBoundingRect() ,Qt::KeepAspectRatio);@

        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved