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 know when cursor is out of qlabel when clicked??
QtWS25 Last Chance

How to know when cursor is out of qlabel when clicked??

Scheduled Pinned Locked Moved Solved General and Desktop
qlabelmouseevent
8 Posts 3 Posters 3.9k 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.
  • V Offline
    V Offline
    vasu_gupta
    wrote on 26 Nov 2017, 09:50 last edited by aha_1980 1 Dec 2019, 12:35
    #1

    i want to select roi in a qlable such that in case the operator get out of bound while selecting the roi i can display a warning messge box.

    right now i have tried this:

    https://www.youtube.com/watch?v=d0CDMtfefB4

    in which he has used clickablelable class then promote the qlabel,
    its working fine as long as i am selecting the roi within the box.

    however the leave event only works when mouse is not pressed i.e in case the user came out of qlabel while selecting roi(mouse button pressed) the leave signal doesn't get emitted .

    So my Question is what to do so when the user tries to select roi out of qlable, program can emit some signal

    1 Reply Last reply
    0
    • S SGaist
      26 Nov 2017, 21:03

      Hi,

      Sounds rather like a job for QRubberBand. There's an example in the documentation.

      V Offline
      V Offline
      vasu_gupta
      wrote on 27 Nov 2017, 06:18 last edited by
      #7

      @SGaist ,@mrjj

      I am using the clickablelable class and then promoting some qlables on which i have to select roi to this class

      clickablelabel.h :

      #ifndef CLICKABLELABEL_H
      #define CLICKABLELABEL_H
      
      #include <QLabel>
      #include <QWidget>
      #include <Qt>
      #include<QMouseEvent>
      #include<QEvent>
      #include<QRubberBand>
      class ClickableLabel : public QLabel { 
          Q_OBJECT 
      
      public:
          explicit ClickableLabel(QWidget* parent = Q_NULLPTR, Qt::WindowFlags f = Qt::WindowFlags());
          ~ClickableLabel();
          void mousePressEvent(QMouseEvent* event);
          void mouseMoveEvent (QMouseEvent* event);
          void mouseReleaseEvent(QMouseEvent* event);
          //void leaveEvent(QEvent *event);
          int x_ini,y_ini,x_new,y_new,width_roi,height_roi;
          QPoint origin;
          QRubberBand rubberBand;
      
      signals:
          void Mouse_pressed();
          void Mouse_pos();
          void Mouse_released();
        //  void Mouse_left();
      
      protected:
      
      };
      
      #endif // CLICKABLELABEL_H
      

      clickablelable.cpp:

      #include "ClickableLabel.h"
      
      ClickableLabel::ClickableLabel(QWidget* parent, Qt::WindowFlags f)
          : QLabel(parent) {
          
      }
      
      ClickableLabel::~ClickableLabel() {}
      
      void ClickableLabel::mousePressEvent(QMouseEvent *event)
      {
          this->x_ini = event->x();
          this->y_ini = event->y();
          origin=event->pos();
          if(!rubberBand)
          {
              rubberBand= new QRubberBand(QRubberBand::Rectangle,this);
          }
          rubberBand.setGeometry(QRect(origin,QSize()));
          rubberBand.show();
      
         emit Mouse_pressed();
      }
      void ClickableLabel::mouseMoveEvent(QMouseEvent *event)
      {
          this->x_new = event->x();
          this->y_new = event->y();
          rubberBand.setGeometry(QRect(origin,event->pos()).normalized());
          emit Mouse_pos();
      }
      
      void ClickableLabel::mouseReleaseEvent(QMouseEvent *event)
      {
          this->width_roi=abs(event->x()-x_ini);
          this->height_roi=abs(event->y()-y_ini);
          rubberBand.hide();
          emit Mouse_released();
      }
      /*
      void ClickableLabel::leaveEvent(QEvent *event)
      {
          emit Mouse_left();
      }
      
      */
      

      And i am getting these error while compiling:

      /home/svs/OSSM _single structutr_v5/GUI/ClickableLabel.cpp:4: error: no matching function for call to ‘QRubberBand::QRubberBand()’
           : QLabel(parent) {
      
      /home/svs/OSSM _single structutr_v5/GUI/ClickableLabel.cpp:15: error: no match for ‘operator!’ (operand type is ‘QRubberBand’)
           if(!rubberBand)
              ^
      
      /home/svs/OSSM _single structutr_v5/GUI/ClickableLabel.cpp:17: error: no match for ‘operator=’ (operand types are ‘QRubberBand’ and ‘QRubberBand*’)
               rubberBand= new QRubberBand(QRubberBand::Rectangle,this);
                         ^
      

      now can you tell me what i am doing wrong

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 26 Nov 2017, 10:23 last edited by
        #2

        Hi
        You title is a little confusing.

        You mean: (?)
        How to catch if user press and HOLD mouse button over a QLabel (that is made clickable) and then move mouse outside Label and release it ?

        Buttons use
        http://doc.qt.io/qt-5/qwidget.html#grabMouse
        so that they will get the mouseRelease event so they wont stay drawn pushed down if user move cursor without letting go of button.
        Be carefull with it and make sure to release the grab.

        • get out of bound while selecting the roi i can display a warning messge box.

        Im not sure how user can get out of bounds selecting something.

        1 Reply Last reply
        0
        • V Offline
          V Offline
          vasu_gupta
          wrote on 26 Nov 2017, 15:43 last edited by
          #3

          sorry for being confusing....
          when a push button is clicked i activate connection mentioned below so that i can select roi in the qlabel :

          connect(ui->IMAGE_CAM_2,SIGNAL(Mouse_pressed()),this,SLOT(Mouse_pressed()));
          
          connect(ui->IMAGE_CAM_2,SIGNAL(Mouse_released()),this,SLOT(Mouse_released()));
          

          once mouse button is release it disconnect the above mentioned connection.

          Now after forming connection(clicking pushbutton) there are 2 possibility of error :-

          1. user might press mousebutton outside of the qlabel
          2. after pressing mousebutton in the qlabel boundary he might by mistake release the mousebutton outside of the qlable region.

          In any of the above mentioned case i want to display a warning message to select roi in the qlable region only ,this can easily be done if i can get a signal so what i want to know is how i can get that signal or if there is any other way you can think of to safeguard above mentioned scenario.

          M 1 Reply Last reply 26 Nov 2017, 15:53
          0
          • V vasu_gupta
            26 Nov 2017, 15:43

            sorry for being confusing....
            when a push button is clicked i activate connection mentioned below so that i can select roi in the qlabel :

            connect(ui->IMAGE_CAM_2,SIGNAL(Mouse_pressed()),this,SLOT(Mouse_pressed()));
            
            connect(ui->IMAGE_CAM_2,SIGNAL(Mouse_released()),this,SLOT(Mouse_released()));
            

            once mouse button is release it disconnect the above mentioned connection.

            Now after forming connection(clicking pushbutton) there are 2 possibility of error :-

            1. user might press mousebutton outside of the qlabel
            2. after pressing mousebutton in the qlabel boundary he might by mistake release the mousebutton outside of the qlable region.

            In any of the above mentioned case i want to display a warning message to select roi in the qlable region only ,this can easily be done if i can get a signal so what i want to know is how i can get that signal or if there is any other way you can think of to safeguard above mentioned scenario.

            M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 26 Nov 2017, 15:53 last edited by mrjj
            #4

            Hi

            1:
            As in user totally click/miss the area where he should hit and click on something else in the window?
            and you would like to catch this situation ?
            An event filter could be used so no matter where he clicks u get to know.

            2:
            If you want the mouseReleased to go back to the widget where u started the mouse Press, you can use the grabmouse call.
            That would help as no matter where he releases it, it goes back to same widget.

            its not clear to me what "select roi in the qlable region " really means.

            Normally labels just display text so im not sure what user does to them.
            It sounds like he can click on them and then something is selected.
            So its sort of a clickable label u turn on when user press "SELECT" and then
            he can click on a label ?

            is IMAGE_CAM_2 the label`?

            V 1 Reply Last reply 26 Nov 2017, 17:36
            0
            • M mrjj
              26 Nov 2017, 15:53

              Hi

              1:
              As in user totally click/miss the area where he should hit and click on something else in the window?
              and you would like to catch this situation ?
              An event filter could be used so no matter where he clicks u get to know.

              2:
              If you want the mouseReleased to go back to the widget where u started the mouse Press, you can use the grabmouse call.
              That would help as no matter where he releases it, it goes back to same widget.

              its not clear to me what "select roi in the qlable region " really means.

              Normally labels just display text so im not sure what user does to them.
              It sounds like he can click on them and then something is selected.
              So its sort of a clickable label u turn on when user press "SELECT" and then
              he can click on a label ?

              is IMAGE_CAM_2 the label`?

              V Offline
              V Offline
              vasu_gupta
              wrote on 26 Nov 2017, 17:36 last edited by
              #5

              @mrjj
              ROI stands for "region of interest" which you can say is a small portion of qlabel area .I am displaing a frame in the qlabel IMAGE_CAM_2 using setpixmap function,once the frame is loaded on the qlabel as you said i click on a SELECT button after which he can select a small region on the qlabel.

              forgot about the 1st scenario as it seems unless i mousepress on qlabel nothing will happen so it somewhat solved,now the only issue is that if the user while selecting the ROI gets out of boundary of the qlabel a signal for warning gets emitted

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 26 Nov 2017, 21:03 last edited by
                #6

                Hi,

                Sounds rather like a job for QRubberBand. There's an example in the documentation.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                V 1 Reply Last reply 27 Nov 2017, 06:18
                0
                • S SGaist
                  26 Nov 2017, 21:03

                  Hi,

                  Sounds rather like a job for QRubberBand. There's an example in the documentation.

                  V Offline
                  V Offline
                  vasu_gupta
                  wrote on 27 Nov 2017, 06:18 last edited by
                  #7

                  @SGaist ,@mrjj

                  I am using the clickablelable class and then promoting some qlables on which i have to select roi to this class

                  clickablelabel.h :

                  #ifndef CLICKABLELABEL_H
                  #define CLICKABLELABEL_H
                  
                  #include <QLabel>
                  #include <QWidget>
                  #include <Qt>
                  #include<QMouseEvent>
                  #include<QEvent>
                  #include<QRubberBand>
                  class ClickableLabel : public QLabel { 
                      Q_OBJECT 
                  
                  public:
                      explicit ClickableLabel(QWidget* parent = Q_NULLPTR, Qt::WindowFlags f = Qt::WindowFlags());
                      ~ClickableLabel();
                      void mousePressEvent(QMouseEvent* event);
                      void mouseMoveEvent (QMouseEvent* event);
                      void mouseReleaseEvent(QMouseEvent* event);
                      //void leaveEvent(QEvent *event);
                      int x_ini,y_ini,x_new,y_new,width_roi,height_roi;
                      QPoint origin;
                      QRubberBand rubberBand;
                  
                  signals:
                      void Mouse_pressed();
                      void Mouse_pos();
                      void Mouse_released();
                    //  void Mouse_left();
                  
                  protected:
                  
                  };
                  
                  #endif // CLICKABLELABEL_H
                  

                  clickablelable.cpp:

                  #include "ClickableLabel.h"
                  
                  ClickableLabel::ClickableLabel(QWidget* parent, Qt::WindowFlags f)
                      : QLabel(parent) {
                      
                  }
                  
                  ClickableLabel::~ClickableLabel() {}
                  
                  void ClickableLabel::mousePressEvent(QMouseEvent *event)
                  {
                      this->x_ini = event->x();
                      this->y_ini = event->y();
                      origin=event->pos();
                      if(!rubberBand)
                      {
                          rubberBand= new QRubberBand(QRubberBand::Rectangle,this);
                      }
                      rubberBand.setGeometry(QRect(origin,QSize()));
                      rubberBand.show();
                  
                     emit Mouse_pressed();
                  }
                  void ClickableLabel::mouseMoveEvent(QMouseEvent *event)
                  {
                      this->x_new = event->x();
                      this->y_new = event->y();
                      rubberBand.setGeometry(QRect(origin,event->pos()).normalized());
                      emit Mouse_pos();
                  }
                  
                  void ClickableLabel::mouseReleaseEvent(QMouseEvent *event)
                  {
                      this->width_roi=abs(event->x()-x_ini);
                      this->height_roi=abs(event->y()-y_ini);
                      rubberBand.hide();
                      emit Mouse_released();
                  }
                  /*
                  void ClickableLabel::leaveEvent(QEvent *event)
                  {
                      emit Mouse_left();
                  }
                  
                  */
                  

                  And i am getting these error while compiling:

                  /home/svs/OSSM _single structutr_v5/GUI/ClickableLabel.cpp:4: error: no matching function for call to ‘QRubberBand::QRubberBand()’
                       : QLabel(parent) {
                  
                  /home/svs/OSSM _single structutr_v5/GUI/ClickableLabel.cpp:15: error: no match for ‘operator!’ (operand type is ‘QRubberBand’)
                       if(!rubberBand)
                          ^
                  
                  /home/svs/OSSM _single structutr_v5/GUI/ClickableLabel.cpp:17: error: no match for ‘operator=’ (operand types are ‘QRubberBand’ and ‘QRubberBand*’)
                           rubberBand= new QRubberBand(QRubberBand::Rectangle,this);
                                     ^
                  

                  now can you tell me what i am doing wrong

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 27 Nov 2017, 07:02 last edited by
                    #8

                    Hi
                    should that not be pointer ?
                    QRubberBand rubberBand; -> QRubberBand * rubberBand;
                    (since you do rubberBand= new QRubberBand(QRubberBand::Rectangle,this) )

                    that would explain all 3 errors.

                    1 Reply Last reply
                    2

                    3/8

                    26 Nov 2017, 15:43

                    topic:navigator.unread, 5
                    • Login

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