3
Hello, I recently had a question about how to track coordinates of a Qlabel using the mouse cursor. It worked fine for what I was thinking of doing. What I intend to do is the following, I have an image that project on the screen through a Qlabel, included a function to track the coordinates of this Qlabel. Below a screen Printscreen of my project:
But I will need the values of these correct image coordinates to work on in the future, but the only values the cursor gets are the pixel values the size of my Qlabel. For example, Qlabel is 551x461 in size, while the image is 960x720 in size. Is there any way I can capture the correct pixel coordinates of the image on Qlabel with different size? Is there any method of converting this data into QT? I understand that this problem seems to be too complicated, but I thought it would not cost to try.
Below the Coordinates capture code:
void mainwindow::on_ButtonAddFileira_clicked(){
this->pTimer.disconnect();
QImage image;
image.load("C:/Users/Syn/Desktop/TCC/Fotos Bolsão FEI/02 Bolsao_Cheio_Foreground.jpg");
//image = MatToQImage(this->ImagemBase.capturaImgBuffer());
ui->labelScreen->setPixmap(QPixmap::fromImage(image));
ui->labelScreen->setScaledContents(true);
connect(&cursorTimer, SIGNAL(timeout()), SLOT(cursor_timer_timeout()));
cursorTimer.start(50);
}
labelScreen is the Image Display Qlabel, and in the code below label_X and label_Y are the coordinates Labels on the screen.
void mainwindow::cursor_timer_timeout(){
QPoint cursorPos = ui->labelScreen->mapFromGlobal( QCursor::pos() );
int x = cursorPos.x();
int y = cursorPos.y();
ui->label_X->setNum(x);
ui->label_Y->setNum(y);
}
Thanks in advance for the help and attention, any little help will be welcome.
Thank you, it helped a lot. I was able to get the image coordinates just by the rule of three. Really, I did not take into account the reason of aspect, I am beginner in manipulation of images, so I had no idea of this concept, more an acquired learning. The label had this bizarre size because I didn’t want to create a spacious window of my project, and I created the size of the label compatible with the screen. Again, thank you so much for your help friend!
– Yuri Pires