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. Load image in custom QGraphicsView

Load image in custom QGraphicsView

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 934 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

    Hi,
    I am trying to load an image (QPixmap) into a custom QGraphicsView.
    I made a design using the .ui form and promoted the GraphicsView to my custom graphicsView.
    It allows me to select a photo, but the photo is not visible in the GraphicsView.
    Is there anyone who knows what this problem is?

    this is the code:

    mainwindow.h:

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QGraphicsView>
    #include <QGraphicsScene>
    #include <QScrollArea>
    #include <QGraphicsPixmapItem>
    #include <QGraphicsPolygonItem>
    #include <QGraphicsItem>
    #include <QFileDialog>
    #include <QDir>
    
    #include <QDebug>
    
    QT_BEGIN_NAMESPACE
    namespace Ui { class MainWindow; }
    QT_END_NAMESPACE
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    
    protected slots:
        void openPhoto();
    private slots:
        void on_pushButton_clicked();
    
    private:
        Ui::MainWindow *ui;
        void createMenus();
        void createActions();
    
        QMenu *fileMenu;
        QMenu *optionMenu;
    
        QAction *openAct;
        QAction *exitAct;
        QAction *clearScreenAct;
        QAction *undoAct;
        QAction *redoAct;
        QAction *addRoomAct;
        QAction *zoomInAct;
        QAction *zoomOutAct;
    
        QGraphicsScene *scene;
    
    };
    #endif // MAINWINDOW_H
    
    

    mainwindow.cpp:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "graphicsview.h"
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        createActions();
        createMenus();
    
        scene = new QGraphicsScene();
    
    
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::createActions()
    {
        openAct = new QAction(tr("&Open..."), this);
        openAct->setShortcuts(QKeySequence::Open);
        connect(openAct, SIGNAL(triggered()), this, SLOT(openPhoto()));
    
        exitAct = new QAction(tr("&Exit"), this);
        exitAct->setShortcuts(QKeySequence::Quit);
        connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
    }
    
    void MainWindow::createMenus()
    {
    
    }
    
    void MainWindow::openPhoto()
    {
        QString fileName;
        fileName = QFileDialog::getOpenFileName(this, tr("Open File"), QDir::currentPath());
        if(!fileName.isNull() || !fileName.isEmpty())
        {
            graphicsView gv;
            gv.openPhoto(fileName);
        }
    }
    
    void MainWindow::on_pushButton_clicked()
    {
        openPhoto();
    }
    
    

    graphicsview.h:

    #ifndef GRAPHICSVIEW_H
    #define GRAPHICSVIEW_H
    
    #include <QWidget>
    #include <QGraphicsView>
    
    class graphicsView : public QGraphicsView
    {
        Q_OBJECT
    public:
        explicit graphicsView(QWidget *parent = nullptr);
        void openPhoto(QString);
    signals:
    
    public slots:
    
    private:
        QGraphicsScene *scene;
    };
    
    #endif // GRAPHICSVIEW_H
    
    

    graphicsview.cpp:

    #include "graphicsview.h"
    
    graphicsView::graphicsView(QWidget *parent) : QGraphicsView(parent)
    {
        scene = new QGraphicsScene();
        this->setScene(scene);
        this->setAlignment(Qt::AlignLeft | Qt::AlignTop);
    }
    
    void graphicsView::openPhoto(QString filename)
    {
        scene->addPixmap(QPixmap(filename));
        this->setScene(scene);
    }
    
    jsulmJ 1 Reply Last reply
    0
    • H hobbyProgrammer

      Hi,
      I am trying to load an image (QPixmap) into a custom QGraphicsView.
      I made a design using the .ui form and promoted the GraphicsView to my custom graphicsView.
      It allows me to select a photo, but the photo is not visible in the GraphicsView.
      Is there anyone who knows what this problem is?

      this is the code:

      mainwindow.h:

      #ifndef MAINWINDOW_H
      #define MAINWINDOW_H
      
      #include <QMainWindow>
      #include <QGraphicsView>
      #include <QGraphicsScene>
      #include <QScrollArea>
      #include <QGraphicsPixmapItem>
      #include <QGraphicsPolygonItem>
      #include <QGraphicsItem>
      #include <QFileDialog>
      #include <QDir>
      
      #include <QDebug>
      
      QT_BEGIN_NAMESPACE
      namespace Ui { class MainWindow; }
      QT_END_NAMESPACE
      
      class MainWindow : public QMainWindow
      {
          Q_OBJECT
      
      public:
          MainWindow(QWidget *parent = nullptr);
          ~MainWindow();
      
      protected slots:
          void openPhoto();
      private slots:
          void on_pushButton_clicked();
      
      private:
          Ui::MainWindow *ui;
          void createMenus();
          void createActions();
      
          QMenu *fileMenu;
          QMenu *optionMenu;
      
          QAction *openAct;
          QAction *exitAct;
          QAction *clearScreenAct;
          QAction *undoAct;
          QAction *redoAct;
          QAction *addRoomAct;
          QAction *zoomInAct;
          QAction *zoomOutAct;
      
          QGraphicsScene *scene;
      
      };
      #endif // MAINWINDOW_H
      
      

      mainwindow.cpp:

      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      #include "graphicsview.h"
      
      MainWindow::MainWindow(QWidget *parent)
          : QMainWindow(parent)
          , ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
      
          createActions();
          createMenus();
      
          scene = new QGraphicsScene();
      
      
      }
      
      MainWindow::~MainWindow()
      {
          delete ui;
      }
      
      void MainWindow::createActions()
      {
          openAct = new QAction(tr("&Open..."), this);
          openAct->setShortcuts(QKeySequence::Open);
          connect(openAct, SIGNAL(triggered()), this, SLOT(openPhoto()));
      
          exitAct = new QAction(tr("&Exit"), this);
          exitAct->setShortcuts(QKeySequence::Quit);
          connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
      }
      
      void MainWindow::createMenus()
      {
      
      }
      
      void MainWindow::openPhoto()
      {
          QString fileName;
          fileName = QFileDialog::getOpenFileName(this, tr("Open File"), QDir::currentPath());
          if(!fileName.isNull() || !fileName.isEmpty())
          {
              graphicsView gv;
              gv.openPhoto(fileName);
          }
      }
      
      void MainWindow::on_pushButton_clicked()
      {
          openPhoto();
      }
      
      

      graphicsview.h:

      #ifndef GRAPHICSVIEW_H
      #define GRAPHICSVIEW_H
      
      #include <QWidget>
      #include <QGraphicsView>
      
      class graphicsView : public QGraphicsView
      {
          Q_OBJECT
      public:
          explicit graphicsView(QWidget *parent = nullptr);
          void openPhoto(QString);
      signals:
      
      public slots:
      
      private:
          QGraphicsScene *scene;
      };
      
      #endif // GRAPHICSVIEW_H
      
      

      graphicsview.cpp:

      #include "graphicsview.h"
      
      graphicsView::graphicsView(QWidget *parent) : QGraphicsView(parent)
      {
          scene = new QGraphicsScene();
          this->setScene(scene);
          this->setAlignment(Qt::AlignLeft | Qt::AlignTop);
      }
      
      void graphicsView::openPhoto(QString filename)
      {
          scene->addPixmap(QPixmap(filename));
          this->setScene(scene);
      }
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @hobbyProgrammer said in Load image in custom QGraphicsView:

      graphicsView gv;

      You're creating a LOCAL instance of graphicsView which is NOT used anywhere afterwards...

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

      H 1 Reply Last reply
      1
      • H Offline
        H Offline
        hobbyProgrammer
        wrote on last edited by
        #3

        made the code working...
        this is what it looks like now:

        mainwindow.h:

        #ifndef MAINWINDOW_H
        #define MAINWINDOW_H
        
        #include <QMainWindow>
        #include <QGraphicsView>
        #include <QGraphicsScene>
        #include <QScrollArea>
        #include <QGraphicsPixmapItem>
        #include <QGraphicsPolygonItem>
        #include <QGraphicsItem>
        #include <QFileDialog>
        #include <QDir>
        
        #include <QDebug>
        
        QT_BEGIN_NAMESPACE
        namespace Ui { class MainWindow; }
        QT_END_NAMESPACE
        
        class MainWindow : public QMainWindow
        {
            Q_OBJECT
        
        public:
            MainWindow(QWidget *parent = nullptr);
            ~MainWindow();
        
        protected slots:
            void openPhoto();
        private slots:
            void on_pushButton_clicked();
        
        private:
            Ui::MainWindow *ui;
            void createMenus();
            void createActions();
        
            QMenu *fileMenu;
            QMenu *optionMenu;
        
            QAction *openAct;
            QAction *exitAct;
            QAction *clearScreenAct;
            QAction *undoAct;
            QAction *redoAct;
            QAction *addRoomAct;
            QAction *zoomInAct;
            QAction *zoomOutAct;
            QAction *drawAct;
        
            QGraphicsScene *scene;
            QGraphicsItem *item;
        
        };
        #endif // MAINWINDOW_H
        
        

        mainwindow.cpp:

        #include "mainwindow.h"
        #include "ui_mainwindow.h"
        #include "graphicsview.h"
        
        MainWindow::MainWindow(QWidget *parent)
            : QMainWindow(parent)
            , ui(new Ui::MainWindow)
        {
            ui->setupUi(this);
        
            createActions();
            createMenus();
        
            scene = new QGraphicsScene();
            ui->graphicsView->setScene(scene);
        
        }
        
        MainWindow::~MainWindow()
        {
            delete ui;
        }
        
        void MainWindow::createActions()
        {
            openAct = new QAction(tr("&Open..."), this);
            openAct->setShortcuts(QKeySequence::Open);
            connect(openAct, SIGNAL(triggered()), this, SLOT(openPhoto()));
        
            exitAct = new QAction(tr("&Exit"), this);
            exitAct->setShortcuts(QKeySequence::Quit);
            connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
            
            drawAct = new QAction (tr("&Draw"), this);
        }
        
        void MainWindow::createMenus()
        {
        
        }
        
        void MainWindow::openPhoto()
        {
            QString fileName;
            fileName = QFileDialog::getOpenFileName(this, tr("Open File"), QDir::currentPath());
            QImage image(fileName);
            item = new QGraphicsPixmapItem(QPixmap::fromImage(image));
            scene->addItem(item);
        }
        
        void MainWindow::on_pushButton_clicked()
        {
            openPhoto();
        }
        

        graphicsview.h:

        #ifndef GRAPHICSVIEW_H
        #define GRAPHICSVIEW_H
        
        #include <QWidget>
        #include <QGraphicsView>
        #include <QWheelEvent>
        #include <QDebug>
        
        class graphicsView : public QGraphicsView
        {
            Q_OBJECT
        public:
            explicit graphicsView(QWidget *parent = nullptr);
        protected:
            virtual void wheelEvent(QWheelEvent *event) override;
            void mousePressEvent(QMouseEvent *event) override;
        
        signals:
        
        public slots:
        
        private:
        };
        
        #endif // GRAPHICSVIEW_H
        
        

        graphicsview.cpp:

        #include "graphicsview.h"
        
        graphicsView::graphicsView(QWidget *parent) : QGraphicsView(parent)
        {
        
        }
        
        void graphicsView::wheelEvent(QWheelEvent *event)
        {
            setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
            double scaleFactor = 1.2;
        
            if(event->delta() > 0)
            {
                scale(scaleFactor, scaleFactor);
            }
            else
            {
                scale(1/scaleFactor, 1/scaleFactor);
            }
        }
        
        void graphicsView::mousePressEvent(QMouseEvent *event)
        {
            if(event->button() == Qt::LeftButton)
            {
                qDebug() << "button was pressed";
        
            }
        }
        
        
        
        1 Reply Last reply
        0
        • jsulmJ jsulm

          @hobbyProgrammer said in Load image in custom QGraphicsView:

          graphicsView gv;

          You're creating a LOCAL instance of graphicsView which is NOT used anywhere afterwards...

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

          @jsulm I just update my code, but maybe you have an answer to my next question. How can I check in the mainwindow.cpp the position of the mousepressevent from the graphicsview and if it was a left or right button. I would like to get an immediate signal when the mouse was pressed.

          jsulmJ 1 Reply Last reply
          0
          • H hobbyProgrammer

            @jsulm I just update my code, but maybe you have an answer to my next question. How can I check in the mainwindow.cpp the position of the mousepressevent from the graphicsview and if it was a left or right button. I would like to get an immediate signal when the mouse was pressed.

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @hobbyProgrammer https://doc.qt.io/qt-5/qgraphicsview.html#mousePressEvent

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

            1 Reply Last reply
            4

            • Login

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