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

  • 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:

    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

  • Lifetime Qt Champion

    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
    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.

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


    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.

  • Lifetime Qt Champion


    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.

    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`?

  • @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

  • Lifetime Qt Champion


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

  • @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 :

    #include <QLabel>
    #include <QWidget>
    #include <Qt>
    class ClickableLabel : public QLabel { 
        explicit ClickableLabel(QWidget* parent = Q_NULLPTR, Qt::WindowFlags f = Qt::WindowFlags());
        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;
        void Mouse_pressed();
        void Mouse_pos();
        void Mouse_released();
      //  void Mouse_left();
    #endif // CLICKABLELABEL_H


    #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();
            rubberBand= new QRubberBand(QRubberBand::Rectangle,this);
       emit Mouse_pressed();
    void ClickableLabel::mouseMoveEvent(QMouseEvent *event)
        this->x_new = event->x();
        this->y_new = event->y();
        emit Mouse_pos();
    void ClickableLabel::mouseReleaseEvent(QMouseEvent *event)
        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’)
    /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

  • Lifetime Qt Champion

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

    that would explain all 3 errors.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.