Clearing an image



  • Hi, been struggling and trying to figure out how to clear a image off graphics view, code bellow:
    Documentation didn't seem to help me, I didn't understand it. Want the image to clear out when button close is pressed (see bellow)

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QPixmap>
    #include <QGraphicsScene>
    #include <QFileDialog>
    #include <QtGui>
    #include <string>
    #include <limits>
    #include <vector>
    #include <fstream>
    #include <algorithm>
    #include <iterator>
    #include <cctype>
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    private slots:
        void on_load_clicked();
    
        void on_close_clicked();
    
    private:
        Ui::MainWindow *ui;
        QPixmap image;
        QImage  *imageObject;
        QGraphicsScene *scene;
    };
    
    #endif // MAINWINDOW_H
    
    

    Header file

    Source file :

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <iostream>
    #include <limits>
    #include <vector>
    #include <string>
    #include <fstream>
    #include <algorithm>
    #include <iterator>
    #include <cctype>
    #include <QFileDialog>
    #include <QtGui>
    #include <QPixmap>
    #include <QGraphicsScene>
    #include <QtAlgorithms>
    
    using namespace std;
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::on_load_clicked()
    {
            QString imagePath = QFileDialog::getOpenFileName();
            imageObject = new QImage();
            imageObject->load(imagePath);
            image = QPixmap::fromImage(*imageObject);
            scene = new QGraphicsScene(this);
            scene->addPixmap(image);
            scene->setSceneRect(image.rect());
            ui->graphicsView->setScene(scene);
    }
    
    void MainWindow::on_close_clicked()
    {
    
    }
    
    


  • @CookieMaster
    Do youy want to clear scene ?? Did you try http://doc.qt.io/qt-4.8/qgraphicsscene.html#clear

    Also if you want to clear the View you might need QGraphicsView::viewport().update() http://stackoverflow.com/questions/12439082/qgraphicssceneclear-clearing-scene-but-not-the-view


  • Qt Champions 2016

    @CookieMaster said in Clearing an image:

    clear a image off graphics view,

    You mean , remove it again ?

    Lets see
    QGraphicsPixmapItem * QGraphicsScene::addPixmap(const QPixmap & pixmap)

    It returns QGraphicsPixmapItem pointer to us,

    so if we keep in MainWindow .h (in class)
    like
    QGraphicsPixmapItem * myimageitem;

    Then

    myimageitem = scene->addPixmap(image); // remember item it returns

    Then later we can use
    void QGraphicsScene::removeItem(QGraphicsItem * item)

    removeItem(myimageitem);



  • @mrjj
    What would I put inside of

    void MainWindow::on_close_clicked()
    {
    
    }
    
    

    Also this can be removed from button load im guessing?

     scene->setSceneRect(image.rect());
    

    and I'm only adding this to the header file ?

    QGraphicsPixmapItem * myimageitem;
    
    
    removeItem(myimageitem);
    

    Am I replacing item with image or scene

    Had to do a project in Qt for my final, never used it before so sorry for all the questions.

    Also, reposting the code with everything in the correct order and where it goes may be better :D so I can understand it as well

    Sorry, Just still a bit confused.

    Cant I just do this bellow?

    scene->clear();
    ui->graphicsView->items().clear();
    

  • Qt Champions 2016

    @CookieMaster said in Clearing an image:

    Hi lets see

    What would I put inside of
    void MainWindow::on_close_clicked()

    Well as long as its in other memeber function ( MainWindow::xxxx)
    then it will work the same.

    Also this can be removed from button load im guessing?
    scene->setSceneRect(image.rect());

    Depends on if u want the area to be the same as the image dimensions.

    and I'm only adding this to the header file ?
    QGraphicsPixmapItem * myimageitem;

    Yes as u need the returned pointer to use with removeItem.

    Am I replacing item with image or scene

    The image i assume. You seems to create a new scene for each image loaded.
    This can be the design Or just a sample u saw. Normally , one would keep the scene around
    and just make new views but both is possible.

    http://doc.qt.io/qt-5/graphicsview.html

    Had to do a project in Qt for my final, never used it before so sorry for all the questions.

    That is ok, the forum are here for helping and we don't mind its homework as long as the poster
    shows willingness to learn in the process and not expecting complete solutions.

    Also, reposting the code with everything in the correct order and where it goes may be better :D so I can understand it as well

    Well Its just adding
    QGraphicsPixmapItem * myimageitem;
    as a member so its available to any member function.
    Being a member simply means, adding to the class definition.

    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    private:
        Ui::MainWindow *ui;
    QGraphicsPixmapItem * myimageitem; // memeber
    };
    

    Cant I just do this bellow?

    scene->clear();
    ui->graphicsView->items().clear();
    

    Yes you can if you ONLY insert an image and no other items.
    The clear removes ALL items and will work if u are happy with that.



  • @mrjj

    Thank you so much!!!!


Log in to reply