How to get position or mouse click event in QLabel
-
@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
wrote on 19 Apr 2023, 09:15 last edited by@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:
wrote on 19 Apr 2023, 10:16 last edited by ManiRon28@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 emptywrote on 19 Apr 2023, 10:28 last edited by@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? -
wrote on 19 Apr 2023, 10:46 last edited by
@ManiRon28 Good.
-
@ManiRon28 Good.
wrote on 19 Apr 2023, 10:56 last edited by ManiRon28I 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?
-
@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. -
@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.wrote on 24 Apr 2023, 06:30 last edited by ManiRon28Hi 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
-
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
@ManiRon28 said in How to get position or mouse click event in QLabel:
When I try to get the X and Y coordinate of pixmap I get the values as Zero
This simply means that the pixmap is located at 0,0 inside the label, so what is the problem?
-
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
wrote on 25 Apr 2023, 11:15 last edited by Pl45m4@ManiRon28 said in How to get position or mouse click event in QLabel:
and I found out that the width and height of QLabel is 950400 and the pixmap width is 948398
I think you mean 900x400 (948x398)?!
The 2px difference could be a frame ofQLabel
.I should get coordinates when I press on the image and not on the other part of qlabel which doesnt have image or empty
Simply math / geometry.
You have the image rectangle and you have your mouseClick coordinates.
Edit:
You dont even have to calculate it yourself. There isQRect::contains(QPoint)
So pass your point you get from
clickedLabel(QPoint)
to that check function and do whatever you want to do, if it's inside the image rect (function will return true then).
And how to get the equivalent point on your pixmap, if you want to do something with your image data, @jsulm has explained here:@jsulm said in How to get position or mouse click event in QLabel:
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.
same for y of course
19/24