I would like to do manipulations on the image with OpenCV based on mouseClicks.
I am using QLabel to display cv::Mat images. Now my problem is with getting the mouse clicks positions with respect to the image. So, I would like (0,0) at topleft corner of the image.
Following is my mousePressEvent, but these are not correct co-ordinates.
void MainWindow::mousePressEvent( QMouseEvent* ev )
{
//This seems to work thanks to Pavel
QPoint P = ui->label->mapFrom(this, ev->pos())
//if( ui->label->underMouse() )
{
QMessageBox msgBox;
//m
sgBox.setText(QString("Click Detected X=")+QString::number(mFirstX)+QString(" Y=")+QString::number(mFirstY));
msgBox.setText("x ="+QString::number(P.x()) + " y= " + QString::number(P.y()));
msgBox.exec();
}
}
bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::MouseMove)
{
QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
///////
*/// This seem to still give wrong position, these values do not match to those I get when I /// click
///////
const QPoint P = ui->label->mapFrom(this, mouseEvent->pos());
//statusBar()->showMessage(QString("Mouse move (%1,%2)").arg(mouseEvent->pos().x()).arg(mo
useEvent->pos().y()));
statusBar()->showMessage(QString("Mouse move (%1,%2)").arg(P.x()).arg(P.y()));
}
return false;
}*
Please help.
You need to set QLabel
's alignment to Qt::AlignTop | Qt::AlignLeft
and ensure that its scaledContents
property is false. You should use ui->label->mapFrom(this, ev->pos())
to convert MainWindow coordinates to label coordinates.