在Qt程序窗口上点击任意区域移动窗体
方法是截取组件Widget的鼠标事件函数,自己处理鼠标点击和移动的事件.
Void MyWidget::mouseMoveEvent(QmouseEvent *e)
{
Qpoint newpos=e->globalPos();//获得鼠标相对于屏幕坐标系的位置
Qpooint upLeft=pos0+newpos-last;
Move(upLeft);
}
Void MyWidget::mousePressEvent(QmouseEvent *e)
{
last=e->globalPos();//获得鼠标相对于屏幕坐标系的位置
pos0= e->globalPos()-e->pos();
}
解释:
e->globalPos(); //获得鼠标相对于屏幕坐标系的位置
e->pos(); //鼠标相对于窗体左上角的位置
pos0; //窗体左上角的位置
upLeft; //新的窗体左上角的位置
这里,我们取得的鼠标位置是绝对位置,即相当于窗口的位置,同时也记录下窗体左上角的位置,当鼠标移动时,取得新的绝对位置,则窗口做上角的新位置应该时原来位置与鼠标移动的位置之差.
Junglesong 编辑整理
2004-1-21 |