How to get position or mouse click event in QLabel
-
I have been trying to get the mouse click event on a QLabel
ClickableLabel.h
#ifndef CLICKABLELABEL_H
#define CLICKABLELABEL_H#include <QWidget>
#include <QLabel>class ClickableLabel : public QLabel
{
Q_OBJECTpublic: explicit ClickableLabel(QWidget* parent = 0, Qt::WindowFlags f = Qt::WindowFlags()); ~ClickableLabel(); signals: void clicked(QPoint pos); protected: void mousePressEvent(QMouseEvent* event);
};
#endif // CLICKABLELABEL_HClickableLabel.cpp
#include<ClickableLabel.h>
#include<QMouseEvent>ClickableLabel::ClickableLabel(QWidget* parent, Qt::WindowFlags f): QLabel(parent) {
}
ClickableLabel::~ClickableLabel() {}
void ClickableLabel::mousePressEvent(QMouseEvent* ev)
{
emit clicked(ev->pos());
}Mainwindow.cpp
connect(&obj_ClickLabel,SIGNAL(clicked(QPoint)),this, SLOT(getData(QPoint)));
-
I have been trying to get the mouse click event on a QLabel
ClickableLabel.h
#ifndef CLICKABLELABEL_H
#define CLICKABLELABEL_H#include <QWidget>
#include <QLabel>class ClickableLabel : public QLabel
{
Q_OBJECTpublic: explicit ClickableLabel(QWidget* parent = 0, Qt::WindowFlags f = Qt::WindowFlags()); ~ClickableLabel(); signals: void clicked(QPoint pos); protected: void mousePressEvent(QMouseEvent* event);
};
#endif // CLICKABLELABEL_HClickableLabel.cpp
#include<ClickableLabel.h>
#include<QMouseEvent>ClickableLabel::ClickableLabel(QWidget* parent, Qt::WindowFlags f): QLabel(parent) {
}
ClickableLabel::~ClickableLabel() {}
void ClickableLabel::mousePressEvent(QMouseEvent* ev)
{
emit clicked(ev->pos());
}Mainwindow.cpp
connect(&obj_ClickLabel,SIGNAL(clicked(QPoint)),this, SLOT(getData(QPoint)));
@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? -
@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?@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
-
@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?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_OBJECTpublic: explicit ClickableLabel(QWidget* parent = 0, Qt::WindowFlags f = Qt::WindowFlags()); ~ClickableLabel(); Q_SIGNALS: void clicked(ClickableLabel *click); protected: void mousePressEvent(QMouseEvent* event);
};
#endif // CLICKABLELABEL_HClickableLable.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);
} -
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_OBJECTpublic: explicit ClickableLabel(QWidget* parent = 0, Qt::WindowFlags f = Qt::WindowFlags()); ~ClickableLabel(); Q_SIGNALS: void clicked(ClickableLabel *click); protected: void mousePressEvent(QMouseEvent* event);
};
#endif // CLICKABLELABEL_HClickableLable.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);
}@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.
-
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_OBJECTpublic: explicit ClickableLabel(QWidget* parent = 0, Qt::WindowFlags f = Qt::WindowFlags()); ~ClickableLabel(); Q_SIGNALS: void clicked(ClickableLabel *click); protected: void mousePressEvent(QMouseEvent* event);
};
#endif // CLICKABLELABEL_HClickableLable.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);
}@ManiRon28
If you get theqDebug("DATA");
output then you know theemit 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 usingSIGNAL
/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. -
@ManiRon28
If you get theqDebug("DATA");
output then you know theemit 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 usingSIGNAL
/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.@JonB , @jsulm
Hi
My connect callconnect(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
-
@JonB , @jsulm
Hi
My connect callconnect(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
@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 outpos.x(),pos.y()
, so you do "get values".... -
@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 outpos.x(),pos.y()
, so you do "get values".... -
@JonB no I dont get any values from the X and Y , only the DataVal string gets printed
This is how mu print is coming
@ManiRon28 Please use qDebug properly:
void MainWindow::points(QPoint &pos) { qDebug() << "DataVal" << pos.x() << pos.y(); }
-
@JonB no I dont get any values from the X and Y , only the DataVal string gets printed
This is how mu print is coming
@ManiRon28
That is because your call toqDebug()
is incorrect.qDebug() << "DataVal" << pos.x() << pos.y());
-
@ManiRon28
That is because your call toqDebug()
is incorrect.qDebug() << "DataVal" << pos.x() << pos.y());
-
@ManiRon28 It's all in the documentation:
-
@ManiRon28 It's all in the documentation:
@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 -
@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@ManiRon28
I assume by "image" you meanQLabel::pixmap()
? So you need to find where that is within the label. I don't know, but if you start withqDebug() << this->pixmap()->rect()
what does that tell you, can you get from that where the pixmap is positioned within the label? -
@ManiRon28
I assume by "image" you meanQLabel::pixmap()
? So you need to find where that is within the label. I don't know, but if you start withqDebug() << this->pixmap()->rect()
what does that tell you, can you get from that where the pixmap is positioned within the label? -
@ManiRon28 Good.
-
@ManiRon28 Good.
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
-
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
@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?
-
@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?