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
QtWS25 Last Chance

How to get position or mouse click event in QLabel

Scheduled Pinned Locked Moved Unsolved General and Desktop
24 Posts 4 Posters 6.5k 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.
  • J jsulm
    19 Apr 2023, 05:51

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

    I have been trying to get the mouse click event on a QLabel

    And what does not work?
    Is mousePressEvent called?

    M Offline
    M Offline
    ManiRon28
    wrote on 19 Apr 2023, 06:13 last edited by ManiRon28
    #3

    @jsulm The mouse press event is called but emit signal is not happening, cause I perform an emit , and I have promoted the Qlabel also

    QObject::connect: No such signal ClickableLabel::clicked(ClickableLabel) in mainwindow.cpp:12
    QObject::connect: (receiver name: 'MainWindow')

    I am facing this issue

    1 Reply Last reply
    0
    • J jsulm
      19 Apr 2023, 05:51

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

      I have been trying to get the mouse click event on a QLabel

      And what does not work?
      Is mousePressEvent called?

      M Offline
      M Offline
      ManiRon28
      wrote on 19 Apr 2023, 06:27 last edited by
      #4

      @jsulm

      Actually I made some changes to the code, Kindly refer to this code

      ClickableLabel.h

      #ifndef CLICKABLELABEL_H
      #define CLICKABLELABEL_H

      #include <QWidget>
      #include <QLabel>

      class ClickableLabel : public QLabel
      {
      Q_OBJECT

      public:
          explicit ClickableLabel(QWidget* parent = 0, Qt::WindowFlags f = Qt::WindowFlags());
           ~ClickableLabel();
      
      Q_SIGNALS:
          void clicked(ClickableLabel *click);
      
      protected:
          void mousePressEvent(QMouseEvent* event);
      

      };
      #endif // CLICKABLELABEL_H

      ClickableLable.cpp

      #include<ClickableLabel.h>
      #include<QMouseEvent>

      ClickableLabel::ClickableLabel(QWidget* parent, Qt::WindowFlags f): QLabel(parent) {

      }

      ClickableLabel::~ClickableLabel() {}

      void ClickableLabel::mousePressEvent(QMouseEvent* )
      {
      qDebug("DATA");
      emit clicked(this);
      }

      J J 2 Replies Last reply 19 Apr 2023, 07:06
      0
      • M ManiRon28
        19 Apr 2023, 06:27

        @jsulm

        Actually I made some changes to the code, Kindly refer to this code

        ClickableLabel.h

        #ifndef CLICKABLELABEL_H
        #define CLICKABLELABEL_H

        #include <QWidget>
        #include <QLabel>

        class ClickableLabel : public QLabel
        {
        Q_OBJECT

        public:
            explicit ClickableLabel(QWidget* parent = 0, Qt::WindowFlags f = Qt::WindowFlags());
             ~ClickableLabel();
        
        Q_SIGNALS:
            void clicked(ClickableLabel *click);
        
        protected:
            void mousePressEvent(QMouseEvent* event);
        

        };
        #endif // CLICKABLELABEL_H

        ClickableLable.cpp

        #include<ClickableLabel.h>
        #include<QMouseEvent>

        ClickableLabel::ClickableLabel(QWidget* parent, Qt::WindowFlags f): QLabel(parent) {

        }

        ClickableLabel::~ClickableLabel() {}

        void ClickableLabel::mousePressEvent(QMouseEvent* )
        {
        qDebug("DATA");
        emit clicked(this);
        }

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 19 Apr 2023, 07:06 last edited by
        #5

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

        Actually I made some changes to the code

        And does it work now? If not: what is the issue now? Please provide needed information when asking.

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

        1 Reply Last reply
        0
        • M ManiRon28
          19 Apr 2023, 06:27

          @jsulm

          Actually I made some changes to the code, Kindly refer to this code

          ClickableLabel.h

          #ifndef CLICKABLELABEL_H
          #define CLICKABLELABEL_H

          #include <QWidget>
          #include <QLabel>

          class ClickableLabel : public QLabel
          {
          Q_OBJECT

          public:
              explicit ClickableLabel(QWidget* parent = 0, Qt::WindowFlags f = Qt::WindowFlags());
               ~ClickableLabel();
          
          Q_SIGNALS:
              void clicked(ClickableLabel *click);
          
          protected:
              void mousePressEvent(QMouseEvent* event);
          

          };
          #endif // CLICKABLELABEL_H

          ClickableLable.cpp

          #include<ClickableLabel.h>
          #include<QMouseEvent>

          ClickableLabel::ClickableLabel(QWidget* parent, Qt::WindowFlags f): QLabel(parent) {

          }

          ClickableLabel::~ClickableLabel() {}

          void ClickableLabel::mousePressEvent(QMouseEvent* )
          {
          qDebug("DATA");
          emit clicked(this);
          }

          J Offline
          J Offline
          JonB
          wrote on 19 Apr 2023, 07:07 last edited by JonB
          #6

          @ManiRon28
          If you get the qDebug("DATA"); output then you know the emit clicked(this); will be executed.

          What makes you think it is not? There is nothing in your code as shown to tell you this.

          Show your connect() statement. Show that is executed. And do yourself a favour and stop using SIGNAL/SLOT() old-style connection syntax, there is a reason it was replaced a decade ago....

          P.S.
          Please use the forum's Code tags around your code, it makes it easier for others.

          M 1 Reply Last reply 19 Apr 2023, 08:47
          1
          • J JonB
            19 Apr 2023, 07:07

            @ManiRon28
            If you get the qDebug("DATA"); output then you know the emit clicked(this); will be executed.

            What makes you think it is not? There is nothing in your code as shown to tell you this.

            Show your connect() statement. Show that is executed. And do yourself a favour and stop using SIGNAL/SLOT() old-style connection syntax, there is a reason it was replaced a decade ago....

            P.S.
            Please use the forum's Code tags around your code, it makes it easier for others.

            M Offline
            M Offline
            ManiRon28
            wrote on 19 Apr 2023, 08:47 last edited by ManiRon28
            #7

            @JonB , @jsulm
            Hi
            My connect call

            connect(ui->label,SIGNAL(clickedlabel(QPoint &)),this, SLOT(points(QPoint &)));
            

            Clicklabel.h

            #ifndef CLICKLABEL_H
            #define CLICKLABEL_H
            
            #include <QLabel>
            #include <QObject>
            #include <QWidget>
            #include<QMouseEvent>
            #include<QPoint>
            
            class Clicklabel : public QLabel
            {
                Q_OBJECT
            public:
                Clicklabel(QWidget* parent = 0);
                ~Clicklabel();
            
            protected:
                void mousePressEvent(QMouseEvent *mouseEvent);
            signals:
                void clickedlabel(QPoint &);
            };
            
            #endif // CLICKLABEL_H
            
            

            Clicklabel.cpp

            void Clicklabel::mousePressEvent(QMouseEvent *mouseEvent)
            {
                QPoint  mouse_pos = mouseEvent->pos();
            
                if(mouse_pos.x() <= this->size().width() && mouse_pos.y()<= this->size().height())
                {
                    if(mouse_pos.x() >0 && mouse_pos.y()>0)
                    {
                        emit clickedlabel(mouse_pos);
                    }
                }
            }
            

            Mainwindow.cpp

            void MainWindow::points(QPoint &pos)
            {
                qDebug("DataVal",pos.x(),pos.y());
            }
            

            Now I am able to view the debug print "DataVal" but I am not able to get the values of X and Y

            J 1 Reply Last reply 19 Apr 2023, 09:05
            0
            • M ManiRon28
              19 Apr 2023, 08:47

              @JonB , @jsulm
              Hi
              My connect call

              connect(ui->label,SIGNAL(clickedlabel(QPoint &)),this, SLOT(points(QPoint &)));
              

              Clicklabel.h

              #ifndef CLICKLABEL_H
              #define CLICKLABEL_H
              
              #include <QLabel>
              #include <QObject>
              #include <QWidget>
              #include<QMouseEvent>
              #include<QPoint>
              
              class Clicklabel : public QLabel
              {
                  Q_OBJECT
              public:
                  Clicklabel(QWidget* parent = 0);
                  ~Clicklabel();
              
              protected:
                  void mousePressEvent(QMouseEvent *mouseEvent);
              signals:
                  void clickedlabel(QPoint &);
              };
              
              #endif // CLICKLABEL_H
              
              

              Clicklabel.cpp

              void Clicklabel::mousePressEvent(QMouseEvent *mouseEvent)
              {
                  QPoint  mouse_pos = mouseEvent->pos();
              
                  if(mouse_pos.x() <= this->size().width() && mouse_pos.y()<= this->size().height())
                  {
                      if(mouse_pos.x() >0 && mouse_pos.y()>0)
                      {
                          emit clickedlabel(mouse_pos);
                      }
                  }
              }
              

              Mainwindow.cpp

              void MainWindow::points(QPoint &pos)
              {
                  qDebug("DataVal",pos.x(),pos.y());
              }
              

              Now I am able to view the debug print "DataVal" but I am not able to get the values of X and Y

              J Offline
              J Offline
              JonB
              wrote on 19 Apr 2023, 09:05 last edited by
              #8

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

              but I am not able to get the values of X and Y

              What does this actually mean? Your qDebug() does print out pos.x(),pos.y(), so you do "get values"....

              M 1 Reply Last reply 19 Apr 2023, 09:11
              0
              • J JonB
                19 Apr 2023, 09:05

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

                but I am not able to get the values of X and Y

                What does this actually mean? Your qDebug() does print out pos.x(),pos.y(), so you do "get values"....

                M Offline
                M Offline
                ManiRon28
                wrote on 19 Apr 2023, 09:11 last edited by ManiRon28
                #9

                @JonB no I dont get any values from the X and Y , only the DataVal string gets printed

                9a0e6f10-3ebe-46f8-a61c-44a78dfcc0f5-image.png

                This is how mu print is coming

                J J 2 Replies Last reply 19 Apr 2023, 09:15
                0
                • M ManiRon28
                  19 Apr 2023, 09:11

                  @JonB no I dont get any values from the X and Y , only the DataVal string gets printed

                  9a0e6f10-3ebe-46f8-a61c-44a78dfcc0f5-image.png

                  This is how mu print is coming

                  J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 19 Apr 2023, 09:15 last edited by
                  #10

                  @ManiRon28 Please use qDebug properly:

                  void MainWindow::points(QPoint &pos)
                  {
                      qDebug() << "DataVal" << pos.x() << pos.y();
                  }
                  

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

                  1 Reply Last reply
                  2
                  • M ManiRon28
                    19 Apr 2023, 09:11

                    @JonB no I dont get any values from the X and Y , only the DataVal string gets printed

                    9a0e6f10-3ebe-46f8-a61c-44a78dfcc0f5-image.png

                    This is how mu print is coming

                    J Offline
                    J Offline
                    JonB
                    wrote on 19 Apr 2023, 09:15 last edited by
                    #11

                    @ManiRon28
                    That is because your call to qDebug() is incorrect.

                    qDebug() << "DataVal" << pos.x() << pos.y());
                    
                    M 1 Reply Last reply 19 Apr 2023, 09:39
                    0
                    • J JonB
                      19 Apr 2023, 09:15

                      @ManiRon28
                      That is because your call to qDebug() is incorrect.

                      qDebug() << "DataVal" << pos.x() << pos.y());
                      
                      M Offline
                      M Offline
                      ManiRon28
                      wrote on 19 Apr 2023, 09:39 last edited by ManiRon28
                      #12

                      @JonB, @jsulm oh sorry thanks for correcting me,

                      I have one more doubt

                      Is there any way to differentiate between left and right mouse click

                      J 1 Reply Last reply 19 Apr 2023, 09:43
                      0
                      • M ManiRon28
                        19 Apr 2023, 09:39

                        @JonB, @jsulm oh sorry thanks for correcting me,

                        I have one more doubt

                        Is there any way to differentiate between left and right mouse click

                        J Offline
                        J Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on 19 Apr 2023, 09:43 last edited by
                        #13

                        @ManiRon28 It's all in the documentation:

                        • https://doc.qt.io/qt-6/qsinglepointevent.html#button
                        • https://doc.qt.io/qt-6/qsinglepointevent.html#buttons

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

                        M 1 Reply Last reply 19 Apr 2023, 10:16
                        0
                        • J jsulm
                          19 Apr 2023, 09:43

                          @ManiRon28 It's all in the documentation:

                          • https://doc.qt.io/qt-6/qsinglepointevent.html#button
                          • https://doc.qt.io/qt-6/qsinglepointevent.html#buttons
                          M Offline
                          M Offline
                          ManiRon28
                          wrote on 19 Apr 2023, 10:16 last edited by ManiRon28
                          #14

                          @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

                          J 1 Reply Last reply 19 Apr 2023, 10:28
                          0
                          • M ManiRon28
                            19 Apr 2023, 10:16

                            @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

                            J Offline
                            J Offline
                            JonB
                            wrote on 19 Apr 2023, 10:28 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 19 Apr 2023, 10:39
                            0
                            • J JonB
                              19 Apr 2023, 10:28

                              @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 19 Apr 2023, 10:39 last edited by
                              #16

                              @JonB Yes the image is loaded using pixmap

                              J 1 Reply Last reply 19 Apr 2023, 10:46
                              0
                              • M ManiRon28
                                19 Apr 2023, 10:39

                                @JonB Yes the image is loaded using pixmap

                                J Offline
                                J Offline
                                JonB
                                wrote on 19 Apr 2023, 10:46 last edited by
                                #17

                                @ManiRon28 Good.

                                M 1 Reply Last reply 19 Apr 2023, 10:56
                                0
                                • J JonB
                                  19 Apr 2023, 10:46

                                  @ManiRon28 Good.

                                  M Offline
                                  M Offline
                                  ManiRon28
                                  wrote on 19 Apr 2023, 10:56 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
                                  
                                  J 1 Reply Last reply 19 Apr 2023, 11:34
                                  0
                                  • M ManiRon28
                                    19 Apr 2023, 10:56

                                    @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
                                    
                                    J Offline
                                    J Offline
                                    jsulm
                                    Lifetime Qt Champion
                                    wrote on 19 Apr 2023, 11:34 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 19 Apr 2023, 11:38
                                    1
                                    • J jsulm
                                      19 Apr 2023, 11:34

                                      @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 19 Apr 2023, 11:38 last edited by ManiRon28
                                      #20

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

                                      J 1 Reply Last reply 19 Apr 2023, 11:49
                                      0
                                      • M ManiRon28
                                        19 Apr 2023, 11:38

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

                                        J Offline
                                        J Offline
                                        jsulm
                                        Lifetime Qt Champion
                                        wrote on 19 Apr 2023, 11:49 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 24 Apr 2023, 06:30
                                        1
                                        • J jsulm
                                          19 Apr 2023, 11:49

                                          @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 24 Apr 2023, 06:30 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

                                          J Pl45m4P 2 Replies Last reply 25 Apr 2023, 06:01
                                          0

                                          12/24

                                          19 Apr 2023, 09:39

                                          • Login

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