1 void MainWindow::mouseMoveEvent(QMouseEvent *event) { 2 3 //获得相对于屏幕的坐标 4 QPoint sPoint1 = event->globalPos(); 5 qDebug() << "相对于屏幕坐标1:" << "(" << sPoint1.rx() << "," << sPoint1.ry() << ")"; 6 7 //获得相对于屏幕的坐标 8 QPoint sPoint2 = QCursor::pos(); 9 qDebug() << "相对于屏幕坐标2:" << "(" << sPoint2.rx() << "," << sPoint2.ry() << ")"; 10 11 //获得相对于窗口的坐标 12 QPoint windowPoint = this->mapFromGlobal(sPoint2); 13 qDebug() << "相对于窗口坐标:" << "(" << windowPoint.rx() << "," << windowPoint.ry() << ")"; 14 15 //获得相对于控件的坐标 16 QPoint widgetPoint = ui->label->mapFromGlobal(sPoint1); 17 qDebug() << "相对于控件坐标:" << "(" << widgetPoint.rx() << "," << widgetPoint.ry() << ")"; 18 }