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. Draw QPixmap in DrawBackground method from QGraphicsView

Draw QPixmap in DrawBackground method from QGraphicsView

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 1.1k 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.
  • H Offline
    H Offline
    hobbyProgrammer
    wrote on last edited by
    #1

    I would like to draw a QPixmap in the drawBackground method of a QGraphicsView class.

    this is my GraphicsView.cpp

    #include "graphicsview.h"
    
    GraphicsView::GraphicsView(QWidget *parent)
        : QGraphicsView(parent)
    {
        scene = new QGraphicsScene(this);
        setScene(scene);
    
    }
    
    void GraphicsView::scaleView(qreal scaleFactor)
    {
        qreal factor = transform().scale(scaleFactor, scaleFactor).mapRect(QRectF(0, 0, 1, 1)).width();
        if (factor < 0.07 || factor > 100)
            return;
    
        scale(scaleFactor, scaleFactor);
    }
    
    void GraphicsView::drawBackground(QPainter *painter, const QRectF &rect)
    {
        Q_UNUSED(rect);
    
        QRectF sceneRect = this->sceneRect();
        painter->drawPixmap(sceneRect, pixmap, pixmap.rect());
    }
    
    void GraphicsView::zoomIn()
    {
        scaleView(qreal(1.2));
    }
    
    void GraphicsView::zoomOut()
    {
        scaleView(1 / qreal(1.2));
    }
    
    void GraphicsView::open()
    {
        QString filename = QFileDialog::getOpenFileName(this,tr("Open File"), QDir::currentPath(), tr("Image Files(*.png *.jpg *.jpeg)"));
        pixmap = QPixmap(filename);
        update();
    }
    
    
    Pl45m4P 1 Reply Last reply
    0
    • H hobbyProgrammer

      I would like to draw a QPixmap in the drawBackground method of a QGraphicsView class.

      this is my GraphicsView.cpp

      #include "graphicsview.h"
      
      GraphicsView::GraphicsView(QWidget *parent)
          : QGraphicsView(parent)
      {
          scene = new QGraphicsScene(this);
          setScene(scene);
      
      }
      
      void GraphicsView::scaleView(qreal scaleFactor)
      {
          qreal factor = transform().scale(scaleFactor, scaleFactor).mapRect(QRectF(0, 0, 1, 1)).width();
          if (factor < 0.07 || factor > 100)
              return;
      
          scale(scaleFactor, scaleFactor);
      }
      
      void GraphicsView::drawBackground(QPainter *painter, const QRectF &rect)
      {
          Q_UNUSED(rect);
      
          QRectF sceneRect = this->sceneRect();
          painter->drawPixmap(sceneRect, pixmap, pixmap.rect());
      }
      
      void GraphicsView::zoomIn()
      {
          scaleView(qreal(1.2));
      }
      
      void GraphicsView::zoomOut()
      {
          scaleView(1 / qreal(1.2));
      }
      
      void GraphicsView::open()
      {
          QString filename = QFileDialog::getOpenFileName(this,tr("Open File"), QDir::currentPath(), tr("Image Files(*.png *.jpg *.jpeg)"));
          pixmap = QPixmap(filename);
          update();
      }
      
      
      Pl45m4P Online
      Pl45m4P Online
      Pl45m4
      wrote on last edited by Pl45m4
      #2

      @hobbyProgrammer

      You want to set an image (instead of just white) as background for your GraphicsView?

      setBackgroundBrush(QImage("PATH"));
      

      Where does your PixMap come from? External image file or painted inside your GraphicsScene as well?

      https://doc.qt.io/qt-5/qgraphicsview.html#drawBackground


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      H 1 Reply Last reply
      1
      • Pl45m4P Pl45m4

        @hobbyProgrammer

        You want to set an image (instead of just white) as background for your GraphicsView?

        setBackgroundBrush(QImage("PATH"));
        

        Where does your PixMap come from? External image file or painted inside your GraphicsScene as well?

        https://doc.qt.io/qt-5/qgraphicsview.html#drawBackground

        H Offline
        H Offline
        hobbyProgrammer
        wrote on last edited by
        #3

        @Pl45m4 okay, but then this should work, right?
        I'm still getting a white background...

        #include "graphicsview.h"
        
        GraphicsView::GraphicsView(QWidget *parent) : QGraphicsView(parent)
        {
            scene = new QGraphicsScene();
            this->setScene(scene);
            this->setAlignment(Qt::AlignLeft | Qt::AlignTop);
            LoadImage();
        }
        
        void GraphicsView::LoadImage()
        {
            filename = QFileDialog::getOpenFileName(this, tr("Open Image"), QDir::currentPath(), tr("Image Files (*.png *.jpg *.jpeg)"));
            this->update();
        }
        
        void GraphicsView::drawBackground(QPainter *painter, const QRectF &rect)
        {
            Q_UNUSED(rect)
            painter->setBackground(QImage(filename));
        }
        
        jsulmJ 1 Reply Last reply
        0
        • H hobbyProgrammer

          @Pl45m4 okay, but then this should work, right?
          I'm still getting a white background...

          #include "graphicsview.h"
          
          GraphicsView::GraphicsView(QWidget *parent) : QGraphicsView(parent)
          {
              scene = new QGraphicsScene();
              this->setScene(scene);
              this->setAlignment(Qt::AlignLeft | Qt::AlignTop);
              LoadImage();
          }
          
          void GraphicsView::LoadImage()
          {
              filename = QFileDialog::getOpenFileName(this, tr("Open Image"), QDir::currentPath(), tr("Image Files (*.png *.jpg *.jpeg)"));
              this->update();
          }
          
          void GraphicsView::drawBackground(QPainter *painter, const QRectF &rect)
          {
              Q_UNUSED(rect)
              painter->setBackground(QImage(filename));
          }
          
          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @hobbyProgrammer It looks like you do not check whether the image was actually loaded successfully.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          H 1 Reply Last reply
          0
          • jsulmJ jsulm

            @hobbyProgrammer It looks like you do not check whether the image was actually loaded successfully.

            H Offline
            H Offline
            hobbyProgrammer
            wrote on last edited by hobbyProgrammer
            #5

            @jsulm @Pl45m4 okay, but it keeps repeating the image and I would like there to be just 1 image, that can be zoomed in and out. Do you know how to do that?

            1 Reply Last reply
            0

            • Login

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