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. QGraphicsView: How to get position relative to parent object?
Forum Updated to NodeBB v4.3 + New Features

QGraphicsView: How to get position relative to parent object?

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 701 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.
  • ademmlerA Offline
    ademmlerA Offline
    ademmler
    wrote on last edited by
    #1

    Hi,

    I have a QgraphicsView + Scene with one Object (QPixmap) inside.
    I try to build a "ExDropper" tool and need to get the "Pixel Position" aka the position relative to the 0,0 coordinate of the placed Pixmap. What I am missing is how to honour the image resolution(like 150dpi) and the scaling of the scene.

    What I have here works in a 1:1 scaling with an 100dpi image.

    void QEnhancedGraphicsView::mouseMoveEvent(QMouseEvent *event)
    {
    // Global coordinate-system, but widget coord. position this should fix it
    //QString text;
    //text = QString("%1 X %2").arg(event->pos().x()).arg(event->pos().y());
    //QToolTip::showText(this->mapToGlobal(QPoint(event->pos().x(), event->pos().y())), text);

    emit signalSendCoordinates(this->mapToParent(event->pos()));
    }

    thx for your hints and help

    JonBJ 1 Reply Last reply
    0
    • ademmlerA ademmler

      Hi,

      I have a QgraphicsView + Scene with one Object (QPixmap) inside.
      I try to build a "ExDropper" tool and need to get the "Pixel Position" aka the position relative to the 0,0 coordinate of the placed Pixmap. What I am missing is how to honour the image resolution(like 150dpi) and the scaling of the scene.

      What I have here works in a 1:1 scaling with an 100dpi image.

      void QEnhancedGraphicsView::mouseMoveEvent(QMouseEvent *event)
      {
      // Global coordinate-system, but widget coord. position this should fix it
      //QString text;
      //text = QString("%1 X %2").arg(event->pos().x()).arg(event->pos().y());
      //QToolTip::showText(this->mapToGlobal(QPoint(event->pos().x(), event->pos().y())), text);

      emit signalSendCoordinates(this->mapToParent(event->pos()));
      }

      thx for your hints and help

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

      @ademmler said in QGraphicsView: How to get position relative to parent object?:

      What I am missing is how to honour the image resolution(like 150dpi) and the scaling of the scene.

      I admit I have not tested, but I don't understand this. You say you want "position relative to parent object" and that is what mapToParent() should give you regardless of scaling/resolution?

      If you do not get someone who answers your question I suggest you provide a complete minimal example which includes the object(s) and the scaling or whatever, show the output from mapToParent() and state what different value you want/expect it to have.

      1 Reply Last reply
      0
      • ademmlerA Offline
        ademmlerA Offline
        ademmler
        wrote on last edited by
        #3

        If I understand you right - what I use should deliver the correct result?
        What I am trying is to map the mouse position on screen to the pixel inside the image file.

        Example: The image might be RGB and has 150dpi resolution.
        I load this a QPixmap into the scene at position 0,0.
        When I hover over the image - I get event-pos().

        The image might be moved or scaled on screen.
        Still I am looking for the correct position/information under the mouse.

        This position will be used to grab a 2x2 or 4x4 "averaged" color values from the given file.

        Pl45m4P 1 Reply Last reply
        0
        • ademmlerA ademmler

          If I understand you right - what I use should deliver the correct result?
          What I am trying is to map the mouse position on screen to the pixel inside the image file.

          Example: The image might be RGB and has 150dpi resolution.
          I load this a QPixmap into the scene at position 0,0.
          When I hover over the image - I get event-pos().

          The image might be moved or scaled on screen.
          Still I am looking for the correct position/information under the mouse.

          This position will be used to grab a 2x2 or 4x4 "averaged" color values from the given file.

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

          @ademmler said in QGraphicsView: How to get position relative to parent object?:

          What I am trying is to map the mouse position on screen to the pixel inside the image file.
          Example: The image might be RGB and has 150dpi resolution.
          I load this a QPixmap into the scene at position 0,0.
          When I hover over the image - I get event-pos().

          An image pixel (pos) is a (logical) image pixel (mind blowing right? :D ) no matter if it actually equals one pixel or a block of four on your physical screen.

          Check what I did here.
          Is it somewhere near what you expect?
          (The whole topic there might be interesting for you)


          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
          2
          • ademmlerA Offline
            ademmlerA Offline
            ademmler
            wrote on last edited by
            #5

            I see - very nice approach. Unfortunately I made a mistake in my wording.

            I have a CMYK Image - on hardisk, which I load into the QGraphicscene as RGB Image.
            By hovering the image I want to get the coordinate, wich I use internally to read this pixel from the file!
            My mapping should be screen coordinate over image to pixel coordinate in file.

            Pl45m4P 1 Reply Last reply
            0
            • ademmlerA ademmler

              I see - very nice approach. Unfortunately I made a mistake in my wording.

              I have a CMYK Image - on hardisk, which I load into the QGraphicscene as RGB Image.
              By hovering the image I want to get the coordinate, wich I use internally to read this pixel from the file!
              My mapping should be screen coordinate over image to pixel coordinate in file.

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

              @ademmler said in QGraphicsView: How to get position relative to parent object?:

              By hovering the image I want to get the coordinate, wich I use internally to read this pixel from the file!

              Either I don't get what you are asking for or you don't understand the solution from the topic I've linked above.

              How should we or Qt know what you do "internally"?
              And only a graphical representation of some image/pixel data has "DPI". Your image on your hard drive does not obviously.
              If you resize the image or change the aspect ratio or something else at runtime, you have to keep that in mind...manually.
              There is no direct Qt "mapping" or function to map any widget (view) mouse position to a certain pixel from some file on your hard drive.

              QImage for example has functions like

              • https://doc.qt.io/qt-6/qimage.html#devicePixelRatio

              or

              • https://doc.qt.io/qt-6/qimage.html#dotsPerMeterX

              which might be helpful for manual translation.


              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
              • ademmlerA Offline
                ademmlerA Offline
                ademmler
                wrote on last edited by
                #7

                Hi @Pl45m4

                Hi, thank you for your ideas and hints.

                From your approach I have understood that you the the pixelValues from the image wich is placed as aQLabel. In this case the simple event->pos maps to the pixel from the image. Right?

                In my approach the image is maybe in a different resolution than the RGB representation on screen.
                Hence I was asking about ideas how to map between the two.

                I ll check the links you gave me if they are helpful.

                Pl45m4P 1 Reply Last reply
                0
                • ademmlerA ademmler

                  Hi @Pl45m4

                  Hi, thank you for your ideas and hints.

                  From your approach I have understood that you the the pixelValues from the image wich is placed as aQLabel. In this case the simple event->pos maps to the pixel from the image. Right?

                  In my approach the image is maybe in a different resolution than the RGB representation on screen.
                  Hence I was asking about ideas how to map between the two.

                  I ll check the links you gave me if they are helpful.

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

                  @ademmler said in QGraphicsView: How to get position relative to parent object?:

                  From your approach I have understood that you the the pixelValues from the image wich is placed as aQLabel. In this case the simple event->pos maps to the pixel from the image. Right?

                  My point was, that no matter how your image is displayed or even printed via some print device (QPrinter) on paper, code like

                      QPixmap pixmapImage("filename.png");
                      pixmapImage.toImage().pixel(X, Y);
                  
                  

                  will always address the "real" pixel ( X / Y ).
                  The only thing you have to do is the translation of the view coordinates, since mapTo or mapFrom does not respect any image transformation you do manually...it's just the plain "parent-child" widget coordinate system 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
                  3

                  • Login

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