【发布时间】:2014-02-12 15:42:18
【问题描述】:
我想使用 OpenCV 基于 mouseClicks 对图像进行操作。
我正在使用 QLabel 来显示 cv::Mat 图像。现在我的问题是让鼠标点击相对于图像的位置。所以,我希望 (0,0) 在图像的左上角。
以下是我的 mousePressEvent,但这些不是正确的坐标。
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;
}*
请帮忙。
【问题讨论】:
-
你应该提供输出,你得到了什么?我也会为此目的使用 QGraphicsScene,因为它更容易定义和使用附加到场景的对象(图像)。
-
您需要将
QLabel的对齐方式设置为Qt::AlignTop | Qt::AlignLeft,并确保其scaledContents属性为false。您应该使用ui->label->mapFrom(this, ev->pos())将MainWindow坐标转换为标签坐标。 -
完美...!非常感谢@PavelStrakhov。
标签: qt qpixmap qlabel mousepress