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. Create QGraphicsItems on top of QGraphicsView from QMainWindow
Forum Updated to NodeBB v4.3 + New Features

Create QGraphicsItems on top of QGraphicsView from QMainWindow

Scheduled Pinned Locked Moved Solved General and Desktop
17 Posts 4 Posters 1.9k Views 2 Watching
  • 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
    #8

    I've rewritten my code to be able to load the image inside of the QGraphicsView, but the canvas stays empty.
    Can someone help me find the mistake?

    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();
    }
    
    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());
    
        graphicsView *gv = new graphicsView();
        gv->openImage(fileName);
    }
    
    void MainWindow::on_pushButton_clicked()
    {
        openPhoto();
    }
    

    graphicsView.cpp

    #include "graphicsview.h"
    
    graphicsView::graphicsView(QWidget *parent) : QGraphicsView(parent)
    {
        scene = new QGraphicsScene();
        this->setScene(scene);
    }
    
    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)
        {
        }
        else if(event->button() == Qt::RightButton)
        {
    
        }
    
    }
    
    void graphicsView::openImage(QString filename)
    {
        QImage image = QImage(filename);
        item = new QGraphicsPixmapItem(QPixmap::fromImage(image));
        scene->addItem(item);
    }
    
    
    A 1 Reply Last reply
    0
    • H hobbyProgrammer

      I've rewritten my code to be able to load the image inside of the QGraphicsView, but the canvas stays empty.
      Can someone help me find the mistake?

      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();
      }
      
      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());
      
          graphicsView *gv = new graphicsView();
          gv->openImage(fileName);
      }
      
      void MainWindow::on_pushButton_clicked()
      {
          openPhoto();
      }
      

      graphicsView.cpp

      #include "graphicsview.h"
      
      graphicsView::graphicsView(QWidget *parent) : QGraphicsView(parent)
      {
          scene = new QGraphicsScene();
          this->setScene(scene);
      }
      
      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)
          {
          }
          else if(event->button() == Qt::RightButton)
          {
      
          }
      
      }
      
      void graphicsView::openImage(QString filename)
      {
          QImage image = QImage(filename);
          item = new QGraphicsPixmapItem(QPixmap::fromImage(image));
          scene->addItem(item);
      }
      
      
      A Offline
      A Offline
      Asperamanca
      wrote on last edited by
      #9

      @hobbyProgrammer said in Create QGraphicsItems on top of QGraphicsView from QMainWindow:

      graphicsView *gv = new graphicsView();
      gv->openImage(fileName);
      

      You create a new view, but you do nothing with it.
      Do you want to pop up a new window every time you open an image? Or do you want to have the GraphicsView as part of the main window?

      H 1 Reply Last reply
      2
      • A Asperamanca

        @hobbyProgrammer said in Create QGraphicsItems on top of QGraphicsView from QMainWindow:

        graphicsView *gv = new graphicsView();
        gv->openImage(fileName);
        

        You create a new view, but you do nothing with it.
        Do you want to pop up a new window every time you open an image? Or do you want to have the GraphicsView as part of the main window?

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

        @Asperamanca since I promoted my widget in the designer, I changed the code to this:

            ui->graphicsView->openImage(fileName);
        

        now it works perfectly! Thank you so much!

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

          Just added this:

          void graphicsView::mousePressEvent(QMouseEvent *event)
          {
              if(event->button() == Qt::LeftButton)
              {
                  QGraphicsEllipseItem *newEllipse = new QGraphicsEllipseItem();
          
                  int x,y;
                  x = event->pos().x();
                  y = event->pos().y();
          
                  newEllipse->setRect(QRectF(x,y,5,5));
                  scene->addItem(newEllipse);
              }
          }
          

          but it results into this:

          70949c9e-f4b0-43f3-9cee-b6a3286b55bd-image.png

          I would really like it if the item showed up on the exact same position where I pressed the mouse.
          I have no clue why it doesn't do that.
          Any ideas?

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

            it seems to be the problem where it wants to draw from the middle instead of from the top left corner. I know that this problem is solvable, but I can't find the solution anywhere.

            A 1 Reply Last reply
            0
            • H hobbyProgrammer

              it seems to be the problem where it wants to draw from the middle instead of from the top left corner. I know that this problem is solvable, but I can't find the solution anywhere.

              A Offline
              A Offline
              Asperamanca
              wrote on last edited by
              #13

              @hobbyProgrammer
              You should really read https://doc.qt.io/qt-5/graphicsview.html# and for this case especially the section "The Graphics View Coordinate System".

              You get your coordinates in view coordinates, but add an item in scene coordinates. The scene auto-sizes to the items you already have in there, unless you override the sceneRect (see QGraphicsScene::sceneRect() for a description of that behavior). So depending on the size of your pixmap, the scene may be zoomed out or zoomed in. It's like watching a soccer game on a TV, and measuring the distance to the goal using a ruler on the TV set.

              If you want the code inside the QGraphicsView, you'll have to use QGraphicsView::mapToScene

              H 1 Reply Last reply
              3
              • H Offline
                H Offline
                hobbyProgrammer
                wrote on last edited by hobbyProgrammer
                #14
                This post is deleted!
                1 Reply Last reply
                0
                • A Asperamanca

                  @hobbyProgrammer
                  You should really read https://doc.qt.io/qt-5/graphicsview.html# and for this case especially the section "The Graphics View Coordinate System".

                  You get your coordinates in view coordinates, but add an item in scene coordinates. The scene auto-sizes to the items you already have in there, unless you override the sceneRect (see QGraphicsScene::sceneRect() for a description of that behavior). So depending on the size of your pixmap, the scene may be zoomed out or zoomed in. It's like watching a soccer game on a TV, and measuring the distance to the goal using a ruler on the TV set.

                  If you want the code inside the QGraphicsView, you'll have to use QGraphicsView::mapToScene

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

                  @Asperamanca
                  Hi,
                  Thank you so much for the explaination.
                  It made me feel like an absolute fool, but that's okay. It's really good to learn and now what everything stands for and you clarified that so much for me using that example.

                  A 1 Reply Last reply
                  0
                  • H hobbyProgrammer

                    @Asperamanca
                    Hi,
                    Thank you so much for the explaination.
                    It made me feel like an absolute fool, but that's okay. It's really good to learn and now what everything stands for and you clarified that so much for me using that example.

                    A Offline
                    A Offline
                    Asperamanca
                    wrote on last edited by
                    #16

                    @hobbyProgrammer
                    Not sure whether your last question is still open. You talk about mapToScene, but you don't use it in your code.

                    You don't sound like a fool to me...to quote John Cleese: "One of the big problems of the world is that the fools are so sure of themselves, and the wise people so full of doubt".
                    If you were a fool, you wouldn't be asking questions, but instead insisting that Qt is stupid, and you didn't need it anyway :-)

                    H 1 Reply Last reply
                    2
                    • A Asperamanca

                      @hobbyProgrammer
                      Not sure whether your last question is still open. You talk about mapToScene, but you don't use it in your code.

                      You don't sound like a fool to me...to quote John Cleese: "One of the big problems of the world is that the fools are so sure of themselves, and the wise people so full of doubt".
                      If you were a fool, you wouldn't be asking questions, but instead insisting that Qt is stupid, and you didn't need it anyway :-)

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

                      @Asperamanca No it's solved (that's why I removed it)

                      "If you were a fool, you wouldn't be asking questions, but instead insisting that Qt is stupid, and you didn't need it anyway :-)"
                      this is litteraly every one of my co-students. They all hate Qt so much, but it needs a lot of time and patience to get right in. I'm gonna keep trying, because I think you can create a lot of different things with it.

                      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