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. How to draw on Image loaded in a QGraphicsView
QtWS25 Last Chance

How to draw on Image loaded in a QGraphicsView

Scheduled Pinned Locked Moved Solved General and Desktop
drawqgraphicsviewc++c++ qtclass
22 Posts 4 Posters 15.6k 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.
  • N Nico1564
    3 Jul 2018, 13:10

    Ok so to do that, i should modify this part of the QtGuiApplication.cpp ?

    	QString fichier = QFileDialog::getOpenFileName(this, "Ouvrir un fichier", QString(), "Images (*.png *.gif *.jpg *.jpeg)");
    	if (fichier.isEmpty())
    	{
    		QMessageBox::critical(this, "Erreur", "Erreur dans la selection de l'image");
    	}
    	else
    	{
    		QMessageBox::information(this, "Fichier", "Vous avez selectionne :\n" + fichier);
    		//create painter with fichier then draw my circle on this painter
    		imgWidget->setImage(QImage(fichier));
    		ui.horizontalLayout->addWidget(imgWidget);
    		defCoos();
    	}
    

    Another question on my circle, once I have drawn it, I could not delete it right ? Or there is a system of layer with QPainter or QImage like we should find in Photoshop for exemple?

    K Offline
    K Offline
    kenchan
    wrote on 3 Jul 2018, 14:03 last edited by
    #12

    @Nico1564
    I just built that sample you used. If you draw something with the painter in the drawOnImage() function you will see it drawn on top of the image.

    N 1 Reply Last reply 3 Jul 2018, 14:05
    0
    • K kenchan
      3 Jul 2018, 14:03

      @Nico1564
      I just built that sample you used. If you draw something with the painter in the drawOnImage() function you will see it drawn on top of the image.

      N Offline
      N Offline
      Nico1564
      wrote on 3 Jul 2018, 14:05 last edited by Nico1564 7 Mar 2018, 14:24
      #13

      @kenchan yes i just test it but it overwrite the first image (wich is a map, let's call it like this if you agree), so i have to set the backgroud of the draw to transparent
      EDIT : even setting the background transparent, the map disappear

      K 1 Reply Last reply 4 Jul 2018, 05:09
      0
      • N Nico1564
        3 Jul 2018, 14:05

        @kenchan yes i just test it but it overwrite the first image (wich is a map, let's call it like this if you agree), so i have to set the backgroud of the draw to transparent
        EDIT : even setting the background transparent, the map disappear

        K Offline
        K Offline
        kenchan
        wrote on 4 Jul 2018, 05:09 last edited by kenchan 7 Apr 2018, 13:36
        #14

        @Nico1564
        Hm, well i just made the sample load the Qt image in the constructor. Then drew a rectangle with the painter in the drawOnImage() functions. That function gets called from the drawForeground() function so it gets refreshed all the time.
        You draw another image but that should get over written by what ever is drawn in the drawOnImage(). Now, I am not sure when you are calling the setBackgroundBrush() function but I would suggest you only call that before you do any other drawing and not after, just to be on the safe side.

        1 Reply Last reply
        1
        • N Nico1564
          3 Jul 2018, 13:10

          Ok so to do that, i should modify this part of the QtGuiApplication.cpp ?

          	QString fichier = QFileDialog::getOpenFileName(this, "Ouvrir un fichier", QString(), "Images (*.png *.gif *.jpg *.jpeg)");
          	if (fichier.isEmpty())
          	{
          		QMessageBox::critical(this, "Erreur", "Erreur dans la selection de l'image");
          	}
          	else
          	{
          		QMessageBox::information(this, "Fichier", "Vous avez selectionne :\n" + fichier);
          		//create painter with fichier then draw my circle on this painter
          		imgWidget->setImage(QImage(fichier));
          		ui.horizontalLayout->addWidget(imgWidget);
          		defCoos();
          	}
          

          Another question on my circle, once I have drawn it, I could not delete it right ? Or there is a system of layer with QPainter or QImage like we should find in Photoshop for exemple?

          A Offline
          A Offline
          Asperamanca
          wrote on 4 Jul 2018, 07:58 last edited by Asperamanca 7 Apr 2018, 07:58
          #15

          @Nico1564 said in How to draw on Image loaded in a QGraphicsView:

          Ok so to do that, i should modify this part of the QtGuiApplication.cpp ?

          	QString fichier = QFileDialog::getOpenFileName(this, "Ouvrir un fichier", QString(), "Images (*.png *.gif *.jpg *.jpeg)");
          	if (fichier.isEmpty())
          	{
          		QMessageBox::critical(this, "Erreur", "Erreur dans la selection de l'image");
          	}
          	else
          	{
          		QMessageBox::information(this, "Fichier", "Vous avez selectionne :\n" + fichier);
          		//create painter with fichier then draw my circle on this painter
          		imgWidget->setImage(QImage(fichier));
          		ui.horizontalLayout->addWidget(imgWidget);
          		defCoos();
          	}
          

          Another question on my circle, once I have drawn it, I could not delete it right ? Or there is a system of layer with QPainter or QImage like we should find in Photoshop for exemple?

          If you want to work with something like layers, then you should not modify the image. GraphicsView is a framework that allows you to create multiple visual objects (images, circles, rectangles, texts,...) and manipulate them individually. You can move objects along x/y axis, you can define a z-order (which object is drawn on top, which below, etc.), and you can define transformations (scaling, rotating,...). You can group objects to they can be manipulated together. A bit like Powerpoint, really, but with code.

          For what you want, it would make sense to create the image, and then a separate circle (QGraphicsEllipseItem) on top of it. Want to delete the circle later on? Just delete the QGraphicsEllipseItem, and your image stays.

          I recommend reading about the basics of the QGraphicsView framework: https://doc.qt.io/qt-5/graphicsview.html

          1 Reply Last reply
          0
          • N Offline
            N Offline
            Nico1564
            wrote on 4 Jul 2018, 15:13 last edited by
            #16

            Ok I have understand (i think) so I just try something in a new project (to see if i have really understood) but I failed and now I'm depressed .... :(

            here is my new code :

            testGraphicView.cpp

            #include "testGraphicView.h"
            
            testGraphicView::testGraphicView(QWidget *parent)
            	: QMainWindow(parent)
            {
            	ui.setupUi(this);
            	scene = new QGraphicsScene(this);
            	ui.graphicsView->setScene(scene);
            	connect(ui.pushButton, SIGNAL(triggered), this, SLOT(Circle()));
            }
            
            void testGraphicView::Circle()
            {
            	ellipse = scene->addEllipse(10, 10, 100, 100);
            
            }
            

            testGraphicView.h

            #pragma once
            
            #include <QtWidgets/QMainWindow>
            #include "ui_testGraphicView.h"
            #include <QGraphicsView>
            
            
            class testGraphicView : public QMainWindow
            {
            	Q_OBJECT
            
            public:
            	testGraphicView(QWidget *parent = Q_NULLPTR);
            
            public slots :
            	void Circle();
            
            private:
            	Ui::testGraphicViewClass ui;
            	QGraphicsScene *scene;
            	QGraphicsEllipseItem *ellipse;
            
            };
            
            K 1 Reply Last reply 4 Jul 2018, 22:20
            0
            • N Nico1564
              4 Jul 2018, 15:13

              Ok I have understand (i think) so I just try something in a new project (to see if i have really understood) but I failed and now I'm depressed .... :(

              here is my new code :

              testGraphicView.cpp

              #include "testGraphicView.h"
              
              testGraphicView::testGraphicView(QWidget *parent)
              	: QMainWindow(parent)
              {
              	ui.setupUi(this);
              	scene = new QGraphicsScene(this);
              	ui.graphicsView->setScene(scene);
              	connect(ui.pushButton, SIGNAL(triggered), this, SLOT(Circle()));
              }
              
              void testGraphicView::Circle()
              {
              	ellipse = scene->addEllipse(10, 10, 100, 100);
              
              }
              

              testGraphicView.h

              #pragma once
              
              #include <QtWidgets/QMainWindow>
              #include "ui_testGraphicView.h"
              #include <QGraphicsView>
              
              
              class testGraphicView : public QMainWindow
              {
              	Q_OBJECT
              
              public:
              	testGraphicView(QWidget *parent = Q_NULLPTR);
              
              public slots :
              	void Circle();
              
              private:
              	Ui::testGraphicViewClass ui;
              	QGraphicsScene *scene;
              	QGraphicsEllipseItem *ellipse;
              
              };
              
              K Offline
              K Offline
              kenchan
              wrote on 4 Jul 2018, 22:20 last edited by
              #17

              @Nico1564 No no, That is not going to work. A QMainWindow is not going to work because it is not a QGraphicsView.
              You must but your QGraphicsView in the central widget of the main window, like the sample you used did.
              The sample is not a bad start but you should simplify it by just removing the functions you don't need for now.

              K 1 Reply Last reply 4 Jul 2018, 23:29
              0
              • K kenchan
                4 Jul 2018, 22:20

                @Nico1564 No no, That is not going to work. A QMainWindow is not going to work because it is not a QGraphicsView.
                You must but your QGraphicsView in the central widget of the main window, like the sample you used did.
                The sample is not a bad start but you should simplify it by just removing the functions you don't need for now.

                K Offline
                K Offline
                kenchan
                wrote on 4 Jul 2018, 23:29 last edited by kenchan 7 Apr 2018, 23:32
                #18

                @Nico1564
                I would suggest you don't call the drawOnImage(painter,imageSize); function.
                Don't add the initial image, just the one you want to add the way you want to add it.
                Add a function to your rOg_image to add the circle or whatever you want to draw on your image, or just draw it directly with the scene functions. Draw it after you have loaded your image.
                Call the draw function you made after you load and draw the image.
                Then you should see what ever you draw on top of the image and you will be able to zoom and scroll after you have zoomed in on the scene.
                Please do as @Asperamanca suggested and read up on the docs for the QGraphicsView and QGraphicsScene.

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  Asperamanca
                  wrote on 5 Jul 2018, 07:00 last edited by
                  #19

                  Here's a minimal GraphicsView sample, that draws an Ellipse:

                  mainwindow.h

                  #ifndef MAINWINDOW_H
                  #define MAINWINDOW_H
                  
                  #include <QWidget>
                  #include <QGraphicsView>
                  #include <QGraphicsScene>
                  
                  class MainWindow : public QWidget
                  {
                      Q_OBJECT
                  
                  public:
                      MainWindow(QWidget *parent = 0);
                      ~MainWindow();
                  
                  private:
                      QGraphicsScene* m_pScene;
                      QGraphicsView* m_pView;
                  };
                  
                  #endif // MAINWINDOW_H
                  

                  mainwindow.cpp

                  #include "mainwindow.h"
                  #include <QGraphicsEllipseItem>
                  
                  MainWindow::MainWindow(QWidget *parent)
                      : QWidget(parent)
                  {
                      m_pScene = new QGraphicsScene(0,0,800,480,this);
                  
                      m_pView = new QGraphicsView(m_pScene, this);
                      m_pView->setFrameStyle(QFrame::NoFrame);
                      m_pView->setGeometry(0,0,800,480);
                      m_pView->setAutoFillBackground(false);
                      m_pView->show();
                  
                      // Add your image here, as a QGraphicsPixmapItem, and it will be drawn below the ellipse
                      QGraphicsEllipseItem* testItem = new QGraphicsEllipseItem(20,30,120,70,0);
                      m_pScene->addItem(testItem);
                  }
                  
                  MainWindow::~MainWindow()
                  {
                  }
                  

                  main.cpp

                  #include <QApplication>
                  #include "mainwindow.h"
                  
                  int main(int argc, char *argv[])
                  {
                      QApplication a(argc, argv);
                      MainWindow w;
                      w.show();
                  
                      return a.exec();
                  }
                  

                  Maybe you can go from there

                  1 Reply Last reply
                  0
                  • N Offline
                    N Offline
                    Nico1564
                    wrote on 5 Jul 2018, 13:28 last edited by Nico1564 7 May 2018, 13:30
                    #20

                    Yeah i know that this work but i need to draw with fonctions. I successed to do that but I don't want to have the GraphicsView at the beginning of the program, and i want to add draws like an Ellipse (and the image) when i click on buttons.

                    K A 2 Replies Last reply 5 Jul 2018, 13:36
                    0
                    • N Nico1564
                      5 Jul 2018, 13:28

                      Yeah i know that this work but i need to draw with fonctions. I successed to do that but I don't want to have the GraphicsView at the beginning of the program, and i want to add draws like an Ellipse (and the image) when i click on buttons.

                      K Offline
                      K Offline
                      kenchan
                      wrote on 5 Jul 2018, 13:36 last edited by kenchan 7 May 2018, 13:47
                      #21

                      @Nico1564
                      Because you have a main window you can add menus, toolbars with buttons or dock widget with buttons. You can make these buttons and menus call your daw functions or what ever you want.
                      The sample you used is a good basis to start from. the sample that @Asperamanca has posted is also a good basis to start from. Now it is up to you to study the docs for the gui elements and do what you need to do.

                      1 Reply Last reply
                      0
                      • N Nico1564
                        5 Jul 2018, 13:28

                        Yeah i know that this work but i need to draw with fonctions. I successed to do that but I don't want to have the GraphicsView at the beginning of the program, and i want to add draws like an Ellipse (and the image) when i click on buttons.

                        A Offline
                        A Offline
                        Asperamanca
                        wrote on 5 Jul 2018, 13:55 last edited by
                        #22

                        @Nico1564 said in How to draw on Image loaded in a QGraphicsView:

                        Yeah i know that this work but i need to draw with fonctions. I successed to do that but I don't want to have the GraphicsView at the beginning of the program, and i want to add draws like an Ellipse (and the image) when i click on buttons.

                        These lines could just as well happen inside a button click

                            QGraphicsEllipseItem* testItem = new QGraphicsEllipseItem(20,30,120,70,0);
                            m_pScene->addItem(testItem);
                        

                        In that case, you would create an ellipse every time you pressed that button.

                        I don't quite understand the "I don't want to have the GraphicsView at the beginning of the program" - maybe you could elaborate on that.

                        1 Reply Last reply
                        0

                        21/22

                        5 Jul 2018, 13:36

                        • Login

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