bool mouse_press; //鼠标按下
void mousePressEvent(QMouseEvent *qevent); //鼠标按下事件
void mouseReleaseEvent(QMouseEvent *qevent); //鼠标释放事件
void mouseMoveEvent(QMouseEvent *qevent); //鼠标移动事件
void RegInfoDlg::mousePressEvent(QMouseEvent *qevent)
{
if(qevent->button() == Qt::LeftButton)
{
mouse_press = true;
//鼠标相对于窗体的位置(或者使用event->globalPos() - this->pos())
move_point = qevent->pos();;
}
}
void RegInfoDlg::mouseMoveEvent(QMouseEvent *qevent)
{
//若鼠标左键被按下
if(mouse_press)
{
//鼠标相对于屏幕的位置
QPoint move_pos = qevent->globalPos();
//移动主窗体位置
this->move(move_pos - move_point);
}
}
void RegInfoDlg::mouseReleaseEvent(QMouseEvent *qevent)
{
//设置鼠标为未被按下
mouse_press = false;
}
这样窗体就能移动了