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. Automatically scaling PixMap to fill entire Graphics view (super grateful for any help)
Forum Update on Monday, May 27th 2025

Automatically scaling PixMap to fill entire Graphics view (super grateful for any help)

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 1.8k 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.
  • R Offline
    R Offline
    Rutten
    wrote on last edited by
    #1

    Hi,

    I have a main window with a graphicsview. I am displaying a pixmap within it using a pixmapgraphics item. I would really like the pixmap to scale up or down automatically as I enlarge the window.
    I've got the graphicsView to grow and shrink as the user changes the size of the window but I don't know how to change the pixamp:

    My Class:

    class MainWindow : public QMainWindow
    {
    Q_OBJECT
    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    void resizeEvent(QResizeEvent *event) override;
    private:
    Ui::MainWindow *ui;
    QGraphicsView *graphicsView;
    QGraphicsScene *scene;
    QGraphicsPixmapItem *pixMapItem;
    QPixmap *myPixMap;
    QImage *image;

    public slots:

    };

    In the .cpp file:

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)

    {
    ui->setupUi( this );
    setWindowTitle("Experiment Scene");

    graphicsView = new MyGraphicsView;
    graphicsView->setWindowTitle(QT_TRANSLATE_NOOP(QGraphicsView, "Test"));
    
    int w = 600;
    int h = 1400;
    
    scene = new QGraphicsScene(0,0,w,h);
    scene->setBackgroundBrush(Qt::black);
    

    // image = new QImage("/Users/virginiarutten/Qt/test.png");

    QPixmap pixmap("/Users/virginiarutten/Qt/test.png");
    
    pixMapItem = new QGraphicsPixmapItem(pixmap.scaled(w,h,Qt::KeepAspectRatio));
    pixMapItem->setFlag(QGraphicsItem::ItemIsMovable);
    pixMapItem->setPos(0,0);
    scene->addItem(pixMapItem);
    
    graphicsView->setScene(scene);
    graphicsView->setDragMode(QGraphicsView::ScrollHandDrag);
    graphicsView->resize(w, h);
    

    // graphicsView->show();
    or
    ui-> graphicsView->show();

    }

    I can only detect when the main window is being changed size using:

    void MainWindow::resizeEvent(QResizeEvent *) {
    qDebug() <<"";
    }

    I also tried just showing the graphics view (no window)
    i.e.: graphicsView->show();

    but in that case I don't know how to detect if the user has increased the size of the window.

    Please please do give any advice - I've been stuck on this problem for hours.

    Many thanks

    FlotisableF 1 Reply Last reply
    0
    • R Rutten

      Hi,

      I have a main window with a graphicsview. I am displaying a pixmap within it using a pixmapgraphics item. I would really like the pixmap to scale up or down automatically as I enlarge the window.
      I've got the graphicsView to grow and shrink as the user changes the size of the window but I don't know how to change the pixamp:

      My Class:

      class MainWindow : public QMainWindow
      {
      Q_OBJECT
      public:
      explicit MainWindow(QWidget *parent = 0);
      ~MainWindow();
      void resizeEvent(QResizeEvent *event) override;
      private:
      Ui::MainWindow *ui;
      QGraphicsView *graphicsView;
      QGraphicsScene *scene;
      QGraphicsPixmapItem *pixMapItem;
      QPixmap *myPixMap;
      QImage *image;

      public slots:

      };

      In the .cpp file:

      MainWindow::MainWindow(QWidget *parent) :
      QMainWindow(parent),
      ui(new Ui::MainWindow)

      {
      ui->setupUi( this );
      setWindowTitle("Experiment Scene");

      graphicsView = new MyGraphicsView;
      graphicsView->setWindowTitle(QT_TRANSLATE_NOOP(QGraphicsView, "Test"));
      
      int w = 600;
      int h = 1400;
      
      scene = new QGraphicsScene(0,0,w,h);
      scene->setBackgroundBrush(Qt::black);
      

      // image = new QImage("/Users/virginiarutten/Qt/test.png");

      QPixmap pixmap("/Users/virginiarutten/Qt/test.png");
      
      pixMapItem = new QGraphicsPixmapItem(pixmap.scaled(w,h,Qt::KeepAspectRatio));
      pixMapItem->setFlag(QGraphicsItem::ItemIsMovable);
      pixMapItem->setPos(0,0);
      scene->addItem(pixMapItem);
      
      graphicsView->setScene(scene);
      graphicsView->setDragMode(QGraphicsView::ScrollHandDrag);
      graphicsView->resize(w, h);
      

      // graphicsView->show();
      or
      ui-> graphicsView->show();

      }

      I can only detect when the main window is being changed size using:

      void MainWindow::resizeEvent(QResizeEvent *) {
      qDebug() <<"";
      }

      I also tried just showing the graphics view (no window)
      i.e.: graphicsView->show();

      but in that case I don't know how to detect if the user has increased the size of the window.

      Please please do give any advice - I've been stuck on this problem for hours.

      Many thanks

      FlotisableF Offline
      FlotisableF Offline
      Flotisable
      wrote on last edited by
      #2

      @Rutten
      Is QGraphicsView::fitInView a choice?

      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