QGraphicsView Qt的点击事件 [英] Click event for QGraphicsView Qt

查看:1750
本文介绍了QGraphicsView Qt的点击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在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有一个环顾一会儿现在,但没有出现什么

解决方案

重新实现 mousePressEvent(QMouseEvent * event)的 QGraphicsView 不是你的小部件将是适当的做的方式。否则你可以用:

  //检测点击是否在视图中。 
QPoint remapped = ui-> graphicsView-> mapFromParent(event-> pos());
if(ui-> graphicsView-> rect()。contains(remapped))
{
QPointF mousePoint = ui-> graphicsView-> mapToScene
}

这假设小部件是 QGraphicsView


I have made a GUI in Qt that is basically a widget with a QGraphicsView on it i have a function:

void GUI::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)
    {
        QPointF mousePoint = ui->graphicsView->mapToScene(event->pos());
        qDebug() << mousePoint;
    }
}

which links to a public slot:

  void mousePressEvent(QMouseEvent *event);

this shows me on the console the x,y coordinate of where i have clicked, however currently this is working on the entire widget and i ideally would like x,y(0,0) to be the top left of the QGraphicsView instead of the top left of the entire widget. does anyone have any idea how to make this work, i thought from my code that this is what it was doing but it turns out this is not so, iv had a look around for a while now but im coming up with nothing

any help would be really appreciated thanks.

解决方案

Reimplement the mousePressEvent(QMouseEvent *event) of the QGraphicsView not your widget would be the 'proper' way of doing it. Otherwise you can hack it with:

// 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 );
}

This assumes that the widget is the parent of the QGraphicsView.

这篇关于QGraphicsView Qt的点击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆