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. How to get position or mouse click event in QLabel
Forum Updated to NodeBB v4.3 + New Features

How to get position or mouse click event in QLabel

Scheduled Pinned Locked Moved Unsolved General and Desktop
24 Posts 4 Posters 6.8k 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.
  • M ManiRon28

    @jsulm, @JonB Thanks for providing me the necessary details

    I have a qlabel and now I am able to detect the click/press event in label, but I want to capture the coordinates based on image size

    For example
    The image size is 100x100 and the qlabel size is 200x200, I should get coordinates when I press on the image and not on the other part of qlabel which doesnt have image or empty

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

    @ManiRon28
    I assume by "image" you mean QLabel::pixmap()? So you need to find where that is within the label. I don't know, but if you start with qDebug() << this->pixmap()->rect() what does that tell you, can you get from that where the pixmap is positioned within the label?

    M 1 Reply Last reply
    0
    • JonBJ JonB

      @ManiRon28
      I assume by "image" you mean QLabel::pixmap()? So you need to find where that is within the label. I don't know, but if you start with qDebug() << this->pixmap()->rect() what does that tell you, can you get from that where the pixmap is positioned within the label?

      M Offline
      M Offline
      ManiRon28
      wrote on last edited by
      #16

      @JonB Yes the image is loaded using pixmap

      JonBJ 1 Reply Last reply
      0
      • M ManiRon28

        @JonB Yes the image is loaded using pixmap

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

        @ManiRon28 Good.

        M 1 Reply Last reply
        0
        • JonBJ JonB

          @ManiRon28 Good.

          M Offline
          M Offline
          ManiRon28
          wrote on last edited by ManiRon28
          #18

          @JonB

          I get the ouput like this

          x = pos.x();
          y=pos.y();
          
              int XVal = ui->labelImage->pixmap().rect().x();
              int YVal = ui->labelImage->pixmap().rect().y();
              int widthVal = ui->labelImage->pixmap().rect().width();
              int heightVal = ui->labelImage->pixmap().rect().height();
          
          
              QString qsVal = "x = " + QString::number(XVal) + ", y = " + QString::number(YVal) + ", Width = " + QString::number(widthVal) + ", height = " + QString::number(heightVal);
          
          x = 522, y =78, Button = LEFT, 
          Rect - x = 0, y = 0, Width = 1379, height = 516
          
          jsulmJ 1 Reply Last reply
          0
          • M ManiRon28

            @JonB

            I get the ouput like this

            x = pos.x();
            y=pos.y();
            
                int XVal = ui->labelImage->pixmap().rect().x();
                int YVal = ui->labelImage->pixmap().rect().y();
                int widthVal = ui->labelImage->pixmap().rect().width();
                int heightVal = ui->labelImage->pixmap().rect().height();
            
            
                QString qsVal = "x = " + QString::number(XVal) + ", y = " + QString::number(YVal) + ", Width = " + QString::number(widthVal) + ", height = " + QString::number(heightVal);
            
            x = 522, y =78, Button = LEFT, 
            Rect - x = 0, y = 0, Width = 1379, height = 516
            
            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #19

            @ManiRon28 If you know where the pixmap is located inside the QLabel and you get the mouse coordinates inside the QLabel then it is simple math to get the coordinates inside the pixmap, right?

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            M 1 Reply Last reply
            1
            • jsulmJ jsulm

              @ManiRon28 If you know where the pixmap is located inside the QLabel and you get the mouse coordinates inside the QLabel then it is simple math to get the coordinates inside the pixmap, right?

              M Offline
              M Offline
              ManiRon28
              wrote on last edited by ManiRon28
              #20

              @jsulm
              Can you please specify the procedure which would be helpful

              jsulmJ 1 Reply Last reply
              0
              • M ManiRon28

                @jsulm
                Can you please specify the procedure which would be helpful

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #21

                @ManiRon28 Come on, this is really basic.
                Let's say xPixmap is the x coordinate of the pixmap inside the QLabel and xMouse is the x coordinate where the use clicked inside the QLabel. Then the x coordinate inside the pixmap is: xMouse - xPixmap.

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                M 1 Reply Last reply
                1
                • jsulmJ jsulm

                  @ManiRon28 Come on, this is really basic.
                  Let's say xPixmap is the x coordinate of the pixmap inside the QLabel and xMouse is the x coordinate where the use clicked inside the QLabel. Then the x coordinate inside the pixmap is: xMouse - xPixmap.

                  M Offline
                  M Offline
                  ManiRon28
                  wrote on last edited by ManiRon28
                  #22

                  @jsulm

                  Hi When I try to get the X and Y coordinate of pixmap I get the values as Zero

                  ui->label->pixmap().rect().x();
                  ui->label->pixmap().rect().y();
                  

                  and I found out that the width and height of QLabel is 950400 and the pixmap width is 948398

                  kindly help me with it

                  jsulmJ Pl45m4P 2 Replies Last reply
                  0
                  • M ManiRon28

                    @jsulm

                    Hi When I try to get the X and Y coordinate of pixmap I get the values as Zero

                    ui->label->pixmap().rect().x();
                    ui->label->pixmap().rect().y();
                    

                    and I found out that the width and height of QLabel is 950400 and the pixmap width is 948398

                    kindly help me with it

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #23

                    @ManiRon28 said in How to get position or mouse click event in QLabel:

                    When I try to get the X and Y coordinate of pixmap I get the values as Zero

                    This simply means that the pixmap is located at 0,0 inside the label, so what is the problem?

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • M ManiRon28

                      @jsulm

                      Hi When I try to get the X and Y coordinate of pixmap I get the values as Zero

                      ui->label->pixmap().rect().x();
                      ui->label->pixmap().rect().y();
                      

                      and I found out that the width and height of QLabel is 950400 and the pixmap width is 948398

                      kindly help me with it

                      Pl45m4P Online
                      Pl45m4P Online
                      Pl45m4
                      wrote on last edited by Pl45m4
                      #24

                      @ManiRon28 said in How to get position or mouse click event in QLabel:

                      and I found out that the width and height of QLabel is 950400 and the pixmap width is 948398

                      I think you mean 900x400 (948x398)?!
                      The 2px difference could be a frame of QLabel.

                      I should get coordinates when I press on the image and not on the other part of qlabel which doesnt have image or empty

                      Simply math / geometry.

                      You have the image rectangle and you have your mouseClick coordinates.

                      • https://www.geeksforgeeks.org/check-if-a-point-lies-on-or-inside-a-rectangle-set-2/

                      Edit:
                      You dont even have to calculate it yourself. There is QRect::contains(QPoint)

                      • https://doc.qt.io/qt-6/qrect.html#contains

                      So pass your point you get from clickedLabel(QPoint) to that check function and do whatever you want to do, if it's inside the image rect (function will return true then).
                      And how to get the equivalent point on your pixmap, if you want to do something with your image data, @jsulm has explained here:

                      @jsulm said in How to get position or mouse click event in QLabel:

                      Let's say xPixmap is the x coordinate of the pixmap inside the QLabel and xMouse is the x coordinate where the use clicked inside the QLabel. Then the x coordinate inside the pixmap is: xMouse - xPixmap.

                      same for y of course


                      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

                      • Login

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