定义头文件
QPoint move_point; //移动的距离
bool mouse_press; //按下鼠标左键
protected:
void mousePressEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
复写实现函数:
void Dialog::mousePressEvent( QMouseEvent * event )
{
//只能是鼠标左键移动和改变大小
if(event->button() == Qt::LeftButton)
{
mouse_press = true;
}
//窗口移动距离
move_point = event->globalPos() - this->pos();
qDebug() << "pos()" << this->pos().x() << " " << this->pos().y() ;
qDebug() << "globalPos()" << event->globalPos().x() << " " << event->globalPos().y() ;
}
void Dialog::mouseReleaseEvent(QMouseEvent *)
{
mouse_press = false;
}
void Dialog::mouseMoveEvent(QMouseEvent *event)
{
//移动窗口
if(mouse_press)
{
QPoint move_pos = event->globalPos();
move(move_pos - move_point);
}
}