Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Load image in custom QGraphicsView



  • 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);
    }
    

  • Lifetime Qt Champion

    @hobbyProgrammer said in Load image in custom QGraphicsView:

    graphicsView gv;

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



  • 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";
    
        }
    }
    
    
    


  • @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.


  • Lifetime Qt Champion


Log in to reply