我在Qt中创建了一个GUI,它基本上是一个带有QGraphicsView的小部件,我有一个函数:
void GUI::mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton)
{
QPointF mousePoint = ui->graphicsView->mapToScene(event->pos());
qDebug() << mousePoint;
}
}
链接到公共广告位:
void mousePressEvent(QMouseEvent *event);
这显示我在控制台上我点击的x,y坐标,但是目前这是在整个小部件上工作,我理想地希望x,y(0,0)成为QGraphicsView的左上角而不是整个小部件的左上角。有没有人知道如何使这项工作,我从我的代码中想到这是它正在做的事情但事实证明这不是这样,iv现在看了一会儿但是我什么都没有
任何帮助都将非常感谢。
答案 0 :(得分:4)
重新实现mousePressEvent(QMouseEvent *event)
的{{1}}而不是您的小部件将是“正确”的方式。否则你可以用它来破解它:
QGraphicsView
这假设小部件是// Detect if the click is in the view.
QPoint remapped = ui->graphicsView->mapFromParent( event->pos() );
if ( ui->graphicsView->rect().contains( remapped ) )
{
QPointF mousePoint = ui->graphicsView->mapToScene( remapped );
}
的父级。