Qt实现鼠标拖动窗口
QPoint m_StartPoint; //鼠标按下起点 bool m_bMousePress; //按下鼠标左键 void CMyUpdateDlg::mouseMoveEvent(QMouseEvent *event) { //移动窗口 if (m_bMousePress) { QPoint curPoint = event->globalPos(); //按住移动时的位置 QPoint movePoint = curPoint - m_StartPoint;
//普通窗口 QPoint mainWinPos = this->pos(); this->move(mainWinPos.x() + movePoint.x(), mainWinPos.y() + movePoint.y());
//标题栏
QPoint mainWinPos = QPoint(parentWidget()->x(), parentWidget()->y());
QPoint pos = mainWinPos + movePoint;
parentWidget()->move(pos);
m_StartPoint = curPoint; } } void CMyUpdateDlg::mousePressEvent(QMouseEvent * event) { //只能是鼠标左键移动和改变大小 if (event->button() == Qt::LeftButton) { m_bMousePress = true; } //按下时鼠标左键时,窗口在屏幕中的坐标 m_StartPoint = event->globalPos(); } void CMyUpdateDlg::mouseReleaseEvent(QMouseEvent * event) { m_bMousePress = false; }