class Widget : public QWidget{ Q_OBJECTpublic: explicit Widget(QWidget *parent = 0); ~Widget();private: Ui::Widget *ui; .... QGraphicsScene scene;protected: virtual void mouseMoveEvent ( QMouseEvent * event );};#endif // WIDGET_H
Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget){ ui->setupUi(this); setMouseTracking(true); ui->graphicsView->setScene(&scene); ...}void Widget::mouseMoveEvent(QMouseEvent* event){ QPointF scenePos = ui->graphicsView->mapToScene(event->pos()); ui->label->setText(QString::number(scenePos.x())+" "+QString::number(scenePos.y()));}
C++ (Qt) QPoint globalPos = this->mapToGlobal( event->pos() ); QPoint scenePos = ui->graphicsView->mapFromGlobal( globalPos ); ui->label->setText(QString::number(scenePos.x())+" "+QString::number(scenePos.y()));
C++ (Qt)QPoint scenePos = ui->graphicsView->mapFromParent( event->pos() );