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:
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
-
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
-
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.
-
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 :-
- user might press mousebutton outside of the qlabel
- 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.
-
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`?
-
@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
-
Hi,
Sounds rather like a job for QRubberBand. There's an example in the documentation.
-
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
-
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.