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. I have a problem placing images on QGraphicsScene

I have a problem placing images on QGraphicsScene

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 3 Posters 568 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.
  • franco.amatoF Offline
    franco.amatoF Offline
    franco.amato
    wrote on last edited by
    #1

    I have a set of QImage objects (which are cuts from a larger image) and after some processing I have to place them on a QGraphicsScene at specific positions. No matter what position I specify, the image is always placed in the center.

    This is the code I use to display every item (QImage)

    void ImageViewer::displayImage(const QImage &image, const QPointF &position, qreal rotationAngle, qreal scaleFactor)
    {
        QPixmap pixmap = QPixmap::fromImage(std::move(image));
        QPainter painter(&pixmap);
        QPen pen(Qt::blue, 1);
        painter.setPen(pen);
        painter.drawRect(pixmap.rect().adjusted(0, 0, -2, -2)); // the border
    
        QGraphicsPixmapItem *item = new QGraphicsPixmapItem(pixmap);
        // Adjust the position to account for the size of the cropped image
        QPointF adjustedPosition = position - QPointF(pixmap.width() / 2.0, pixmap.height() / 2.0);
    
        item->setPos(adjustedPosition); // <- not working
        item->setRotation(rotationAngle);
        item->setScale(scaleFactor);
        addItem(item);
    }
    

    Any idea on what I did wrong?

    Pl45m4P 1 Reply Last reply
    0
    • franco.amatoF franco.amato

      I have a set of QImage objects (which are cuts from a larger image) and after some processing I have to place them on a QGraphicsScene at specific positions. No matter what position I specify, the image is always placed in the center.

      This is the code I use to display every item (QImage)

      void ImageViewer::displayImage(const QImage &image, const QPointF &position, qreal rotationAngle, qreal scaleFactor)
      {
          QPixmap pixmap = QPixmap::fromImage(std::move(image));
          QPainter painter(&pixmap);
          QPen pen(Qt::blue, 1);
          painter.setPen(pen);
          painter.drawRect(pixmap.rect().adjusted(0, 0, -2, -2)); // the border
      
          QGraphicsPixmapItem *item = new QGraphicsPixmapItem(pixmap);
          // Adjust the position to account for the size of the cropped image
          QPointF adjustedPosition = position - QPointF(pixmap.width() / 2.0, pixmap.height() / 2.0);
      
          item->setPos(adjustedPosition); // <- not working
          item->setRotation(rotationAngle);
          item->setScale(scaleFactor);
          addItem(item);
      }
      

      Any idea on what I did wrong?

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by Pl45m4
      #2

      @franco-amato said in I have a problem placing images on QGraphicsScene:

      item->setPos(adjustedPosition); // <- not working
      

      It is probably working, but you just don't see it.
      AFAIK by default, the scene is centering around its first item.
      The more items you add the more the scene will expand, but your view is focused on that first item, at whatever position it might be.

      One solution (there might be other ways) is setting a fixed sceneRect like this:

      QGraphicsView *view;
      // either set view size or a size of your choice.
      imageViewer->setSceneRect ( 0, 0, view->width(), view->height() );
      
      

      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      1 Reply Last reply
      0
      • franco.amatoF Offline
        franco.amatoF Offline
        franco.amato
        wrote on last edited by
        #3

        Not working :( the item is still placed in the center

        Pl45m4P 1 Reply Last reply
        0
        • franco.amatoF franco.amato

          Not working :( the item is still placed in the center

          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on last edited by
          #4

          @franco-amato said in I have a problem placing images on QGraphicsScene:

          Not working :( the item is still placed in the center

          Print the coordinates where you actually set your items to the scene.
          What is postion and adjustedPosition ?


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          1 Reply Last reply
          0
          • franco.amatoF Offline
            franco.amatoF Offline
            franco.amato
            wrote on last edited by
            #5

            Coordinates are 10, 10

            JonBJ 1 Reply Last reply
            0
            • franco.amatoF franco.amato

              Coordinates are 10, 10

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @franco-amato
              I am a little lost. (10, 10) is as near to (0, 0) as damn-it, so wouldn't you expect it to be placed in the center?

              1 Reply Last reply
              1
              • franco.amatoF Offline
                franco.amatoF Offline
                franco.amato
                wrote on last edited by franco.amato
                #7

                I am also lost, I though the (0, 0) coordinates are placed at the top-left corner

                Pl45m4P 1 Reply Last reply
                0
                • franco.amatoF franco.amato

                  I am also lost, I though the (0, 0) coordinates are placed at the top-left corner

                  Pl45m4P Offline
                  Pl45m4P Offline
                  Pl45m4
                  wrote on last edited by
                  #8

                  @franco-amato said in I have a problem placing images on QGraphicsScene:

                  I am also lost, I though the (0, 0) coordinates are placed at the top-left corner

                  The QGraphicsView yes, since it's a QWidget (all QWidgets have a top-left originated coodinate system).
                  However QGraphicsItems have their origin ( 0 / 0 ) at their center point.
                  So it could be possible that you set the scene position but expect a view port position.

                  Read more here

                  • https://doc.qt.io/qt-6/graphicsview.html#the-graphics-view-coordinate-system

                  and here

                  • https://doc.qt.io/qt-6/graphicsview.html#coordinate-mapping

                  If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                  ~E. W. Dijkstra

                  1 Reply Last reply
                  0
                  • JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #9

                    Coordinates of what? Scene, view, item? Top left corner of what? Are you aware that the view positions itself at the center of the scene? I'm not sure where you are looking at center/not center from. Maybe @Pl45m4 understands better.

                    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