1、原理
2、代码
1 void MyWidget::mouseMoveEvent(QMouseEvent *event) 2 { 3 QPoint P3 = event->globalPos(); 4 QPoint P2 = P3 - L; 5 this->move(P2); 6 } 7 8 void MyWidget::mousePressEvent(QMouseEvent *event) 9 { 10 QPoint P3 = event->globalPos(); 11 QPoint P2 = this->geometry().topLeft(); 12 L = P3 - P2; // L是QPoint变量,在.h文件中创建 13 } 14 15 void MyWidget::mouseReleaseEvent(QMouseEvent *) 16 { 17 this->L = QPoint(); 18 }
3、展示