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. Aligning QLabel center in QScrollArea causes wrong coordinate reading

Aligning QLabel center in QScrollArea causes wrong coordinate reading

Scheduled Pinned Locked Moved Unsolved General and Desktop
qlabel positionqscrollareaalignment
6 Posts 2 Posters 982 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.
  • L Offline
    L Offline
    lansing
    wrote on 14 Jun 2020, 09:14 last edited by lansing
    #1

    I have a QLabel for holding picture and the QLabel was placed in a QScrollArea. And I have a eyedropper function that returns color feedback when I mouse over the image. This works when the QLabel was set to align to top and left of the scrollarea, but when I set alignment to AlignCenter, when I mouse over the image, even though the image was displaying in the center, it was actually reading the image as if was still aligning to top and left position.

    For example, I had a white QLabel in a black QScrollArea, the QLabel was100 x 100 and the scrollarea was1000 x 1000. When I aligned center, the label was displayed at between 500 x 500. But when I mouse over it, my eyedropper was reading black. I have to move my mouse to within 100 x 100 from the top left corner in order to pick up the white color again.

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 14 Jun 2020, 09:26 last edited by
      #2

      You most likely forget some coordinate transformation. Please show us some code.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      L 1 Reply Last reply 14 Jun 2020, 11:21
      1
      • C Christian Ehrlicher
        14 Jun 2020, 09:26

        You most likely forget some coordinate transformation. Please show us some code.

        L Offline
        L Offline
        lansing
        wrote on 14 Jun 2020, 11:21 last edited by
        #3

        @Christian-Ehrlicher

        void MyScrollArea::sampleColor(const QPoint & mousePos)
        {
            QPoint pos = imageLabel->mapFromGlobal(mousePos);
        
            QPixmap imagePixmap = imageLabel->pixmap(Qt::ReturnByValue);
            int pixmapWidth = imagePixmap.width();
            int pixmapHeight = imagePixmap.height();
        
            float normX = (float)pos.x() / (float)pixmapWidth;
            float normY = (float)pos.y() / (float)pixmapHeight;
        
            emit signalEyeDropped(normX, normY);
        }
        

        This only works when the label was aligned to top and left.

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 14 Jun 2020, 14:04 last edited by
          #4

          Where does your mousePos comes from? Are you sure that these are global coordinates?

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          L 1 Reply Last reply 14 Jun 2020, 17:42
          1
          • C Christian Ehrlicher
            14 Jun 2020, 14:04

            Where does your mousePos comes from? Are you sure that these are global coordinates?

            L Offline
            L Offline
            lansing
            wrote on 14 Jun 2020, 17:42 last edited by
            #5

            @Christian-Ehrlicher

            I use qDebug() to print out mousePos.x() and mousePos.y() and it's showing my desktop coordinate when I move my mouse around the scrollarea.

            1 Reply Last reply
            0
            • L Offline
              L Offline
              lansing
              wrote on 15 Jun 2020, 05:59 last edited by
              #6

              My sampleColor function was called from the QScrollArea::mouseMoveEvent and the QPoint mousePos was pass in from the mouseEvent there.

              What I'm suspecting is that when the image was loaded, the default alignment AlignTop & AlignLeft kicked in, then the mouseMoveEvent followed and the sampleColor function somehow took a snapshot of the imageLabel, before the AlignCenter setting in the constructor. That's why my mouse was still seeing the image at the pre-aligncenter position in the snapshot.

              1 Reply Last reply
              0

              5/6

              14 Jun 2020, 17:42

              • Login

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