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. [SOLVED] zoom to fit in QGraphicsView
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] zoom to fit in QGraphicsView

Scheduled Pinned Locked Moved General and Desktop
13 Posts 3 Posters 13.1k Views 1 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.
  • J Offline
    J Offline
    jk_mk
    wrote on last edited by
    #1

    Hi,

    I am using Qt designer and I have used a QGraphicsView of size (214,256).I have also subclassed a QGraphicsScene of size (214,256), in order to display a Canvas* scene inside my QGraphicsView.

    @Canvas::Canvas(): QGraphicsScene(0,0,214,256)
    {
    }@

    To display my image I have used this code (where size_x and size_y are the width and height of my image respectively)

    @ if (!pixmapItem_segm) {

     QPixmap tmpmap (QPixmap("result.png"));
        pixmapItem_segm = scene_segmentation->addPixmap ( tmpmap.scaled (size_x,size_y) );
    
            ui->graphicsView_resultImage->setScene(scene_segmentation);
        } else {
    

    QPixmap tmpmap (QPixmap("result.png"));
    pixmapItem_segm->setPixmap(tmpmap.scaled (size_x,size_y));
    }@

    In this case, if the image is smaller than scene, I get my image displayed in the up-left corner of the QGraphicsView.I have also included a mouse press event in order to take the xy cordinates of the pixel.In this case when I press the mouse I get the right values.

    But when I use this to display my image

    @ if (!pixmapItem_segm) {

     QPixmap tmpmap (QPixmap("result.png"));
        pixmapItem_segm = scene_segmentation->addPixmap ( tmpmap.scaled (214,256) );
    
            ui->graphicsView_resultImage->setScene(scene_segmentation);
        } else {
    

    QPixmap tmpmap (QPixmap("result.png"));
    pixmapItem_segm->setPixmap(tmpmap.scaled (214,256));
    }@

    then I display my image on the hole QGraphicsView (as it was my goal), but in this case when I press the mouse event I get the xy coordinates of my QGraphicsScene (214,256), and not the real xy coordinates of my image.
    Do you know how I could zoom my image to fit in my QGraphicsView, but to take the right coordinates every time I press the mouse?

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goblincoding
      wrote on last edited by
      #2

      Perhaps I don't entirely understand your question, but from what you've given here, it seems to me as if you have:

      QGraphicsView coords = QGraphicsScene coords = YourPixMap coords = (214,256)

      So what you're getting is right.

      http://www.goblincoding.com

      1 Reply Last reply
      0
      • J Offline
        J Offline
        jk_mk
        wrote on last edited by
        #3

        To be more specific, lets suppose that I want to diaplay an image of (100,100) and my QGraphicsView coords = QGraphicsScene coords =(214,256).
        Then if I use in my code this

        @tmpmap.scaled (100,100)@

        and press the mouse on my displayed image, I get coordinates between (100,100), this is correct.

        But when I am using this in my code:
        @tmpmap.scaled (214,256)@
        then my image is zoomed to fit to my QGraphicsView, but when I press the mouse I get coordinates (214,256).

        The right would be to take coordinates (100,100), as the real size of my image.Do you know how this could be done?

        I include my code of the mouse press event, in case there is here any mistake

        @void MainWindow::mousejustpressed_x(int z,int y)
        {

        int  k1=size_y-y;
        

        unsigned char value;

        QImage image(scene->sceneRect().size().toSize(),
        QImage::Format_RGB32);

        QPainter painter(&image);
        scene->render(&painter);

        value=image.pixel(z,k1);

        ui->label_13->setText(QString("z:%1").arg(z));
        ui->label_14->setText(QString("y:%1").arg(k1));
        

        ui->label_15->setText(QString("pixel value:%1").arg(value));@

        1 Reply Last reply
        0
        • L Offline
          L Offline
          loladiro
          wrote on last edited by
          #4

          I think what you want to do is use a 100x100 scene, scale that to fit your graphics view with QGraphicsView::scale and then you can use the mapFromScene and mapToSence functions to get what you want. Does that help?

          1 Reply Last reply
          0
          • J Offline
            J Offline
            jk_mk
            wrote on last edited by
            #5

            I have managed to fit my scene to QGraphicsView by using these:

            @Canvas::Canvas(): QGraphicsScene(0,0,214,256)
            {
            }@

            and
            @ if (!pixmapItem) {

            QPixmap tmpmap (QPixmap(fileName, 0, Qt::AutoColor));
                pixmapItem = scene->addPixmap ( tmpmap.scaled (214,256) );
            
                    ui->graphicsView_inputImage->setScene(scene);
                } else {
            

            QPixmap tmpmap (QPixmap(fileName, 0, Qt::AutoColor));
            pixmapItem->setPixmap(tmpmap.scaled (214,256));
            }@

            But still I have the problem of the real coordinates. Now in x coordiantes I am taking these (0,214) and in y coordinates I am taking these (-100,100).Could you explain how to do the mapToScene? What I should write as a press mouse event instead of these?

            @void MainWindow::mousejustpressed(int x,int y)
            {
            int k1=size_y-y;

            unsigned char value;

            QImage image(scene->sceneRect().size().toSize(), 
            QImage::Format_RGB32);
            
            QPainter painter(&image);
            scene->render(&painter);
            
            value=image.pixel(x,k1);
            

            }@

            1 Reply Last reply
            0
            • L Offline
              L Offline
              loladiro
              wrote on last edited by
              #6

              @Canvas::Canvas(): QGraphicsScene(0,0,100,100)
              {
              }
              @

              @QPixmap tmpmap (fileName, 0, Qt::AutoColor);
              if (!pixmapItem) {
              pixmapItem = scene->addPixmap (tmpmap);
              ui->graphicsView_inputImage->setScene(scene);
              ui->graphicsView_inputImage->scale(ui->graphicsView_inputImage->width()/scene->width(),
              ui->graphicsView_inputImage ->height()/scene->height());
              } else {
              pixmapItem->setPixmap(tmpmap);
              }@

              @ void MainWindow::mousejustpressed(int x,int y)
              {

               unsigned char value;
              
                  QImage image(scene->sceneRect().size().toSize(),
                  QImage::Format_RGB32);
              
                  QPainter painter(&image);
                  scene->render(&painter);
              
                  value=image.pixel(ui->graphicsView_inputImage->mapToScene(x,y));
              }
              

              @

              1 Reply Last reply
              0
              • J Offline
                J Offline
                jk_mk
                wrote on last edited by
                #7

                Thanks for your reply, now I am getting to the point.But when I run this code I get one error:

                _ error C2664: 'QRgb QImage::pixel(const QPoint &) const' : cannot convert parameter 1 from 'QPointF' to 'const QPoint &'_

                Do you know how it could be solved?

                1 Reply Last reply
                0
                • L Offline
                  L Offline
                  loladiro
                  wrote on last edited by
                  #8

                  Sorry I forgot the toPoint():
                  @
                  void MainWindow::mousejustpressed(int x,int y)
                  {

                  unsigned char value;

                  QImage image(scene->sceneRect().size().toSize(),
                  QImage::Format_RGB32);
                  
                  QPainter painter(&image);
                  scene->render(&painter);
                  
                  value=image.pixel(ui->graphicsView_inputImage->mapToScene(x,y).toPoint());
                  

                  }@
                  Hope that helps!

                  1 Reply Last reply
                  0
                  • J Offline
                    J Offline
                    jk_mk
                    wrote on last edited by
                    #9

                    Yes, this is it!

                    Thanks a lot for your help

                    1 Reply Last reply
                    0
                    • L Offline
                      L Offline
                      loladiro
                      wrote on last edited by
                      #10

                      Glad I could help. And don't forget to add [Solved] to the title.

                      1 Reply Last reply
                      0
                      • J Offline
                        J Offline
                        jk_mk
                        wrote on last edited by
                        #11

                        How do I add the [Solved]?

                        1 Reply Last reply
                        0
                        • L Offline
                          L Offline
                          loladiro
                          wrote on last edited by
                          #12

                          Click on edit in the original post and then change the title.

                          1 Reply Last reply
                          0
                          • G Offline
                            G Offline
                            goblincoding
                            wrote on last edited by
                            #13

                            Thanks for the question and the input. I definitely got something from this thread :)

                            http://www.goblincoding.com

                            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