Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Qt:qgraphicscene鼠标位置始终(0,0)_C++_Qt - Fatal编程技术网

C++ Qt:qgraphicscene鼠标位置始终(0,0)

C++ Qt:qgraphicscene鼠标位置始终(0,0),c++,qt,C++,Qt,我创建了自己的类,该类继承自QGraphicscene。我还为鼠标事件创建了两种方法。后来我做了qDebug()来检查我的点击位置是否正确,看起来不正确。它总是返回QPoint(0,0) 我试过很多mapfrom的东西,但都没用。有没有办法使这些位置正确工作 一些代码: MyScene.cpp #include "pianoscene.h" #include <QDebug> #include <QGraphicsView> MyScene::MyScene() {

我创建了自己的类,该类继承自QGraphicscene。我还为鼠标事件创建了两种方法。后来我做了qDebug()来检查我的点击位置是否正确,看起来不正确。它总是返回QPoint(0,0)

我试过很多mapfrom的东西,但都没用。有没有办法使这些位置正确工作

一些代码: MyScene.cpp

#include "pianoscene.h"
#include <QDebug>
#include <QGraphicsView>

MyScene::MyScene()
{
    /*setRect(0,0,100,100);
    QGraphicsRectItem *kek = new QGraphicsRectItem;
    QPen pen;
    pen.setColor(Qt::red);
    kek->setRect(0,0,50,50);
    kek->setPen(pen);
    this->addItem(kek);*/
}

void MyScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    QPoint punkt = views().first()->mapFromScene(event->pos().toPoint());
    qDebug()<<"wcisk"<<punkt;
}

void MyScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    qDebug()<<"wcisk"<<event->pos();
}
#包括“pianoscene.h”
#包括
#包括
MyScene::MyScene()
{
/*setRect(0,0100);
QGraphicsRectItem*kek=新的QGraphicsRectItem;
QPen笔;
钢笔颜色(Qt::红色);
kek->setRect(0,0,50,50);
kek->setPen(笔);
本->附加项(kek)*/
}
void MyScene::MousePresseEvent(QGraphicsSceneMouseEvent*事件)
{
QPoint punkt=views().first()->mapFromScene(事件->位置().toPoint());
qDebug()
pos()
包含项目坐标,而不是场景坐标。要获取场景坐标,请使用
scenePos()

void MyScene::mousePressEvent(qgraphicscscenemouseevent*事件)
{
QPoint punkt=views().first()->mapFromScene(事件->场景点().toPoint());

是的!这就是我要找的;]
void MyScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    QPoint punkt = views().first()->mapFromScene(event->scenePos().toPoint());
    qDebug()<<"wcisk"<<punkt;
}

void MyScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    qDebug()<<"wcisk"<<event->scenePos();
}