Qt 版本 4.8.1 ,主要是为了解决 embeded Qt 下,子窗口的拖动问题。
void MyInputPanel::mousePressEvent(QMouseEvent *mouseEvent) { if(mouseEvent->button() == Qt::LeftButton) { //move 用的是 globalPos, 所以,需要要取出本窗口的(0,0)对应的全局坐标 //pos()返回的是父窗口中位置,不能直接用 startPos = mouseEvent->globalPos() - mapToGlobal(QPoint(0,0)); keyMovePressed = true; } mouseEvent->accept(); } void MyInputPanel::mouseMoveEvent(QMouseEvent *mouseEvent) { if (keyMovePressed) { move(mouseEvent->globalPos() - startPos); } mouseEvent->accept(); } void MyInputPanel::mouseReleaseEvent(QMouseEvent *mouseEvent) { keyMovePressed = false; mouseEvent->accept(); }