1. Cancel the default functions of the qt form (zooming, minimizing, closing the window, dragging the form with the mouse, etc.)
setWindowFlags(Qt::FramelessWindowHint);
Second, realize clicking the mouse to drag the form
1. Add the following code to the *.h file
//头文件加入以下代码
#include <QMouseEvent>
//主体中加入以下代码
private:
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
QPoint diff_pos; //鼠标点击时相对窗体左上角的位移
QPoint mouse_pos; //鼠标点击时的位置
QPoint window_pos; //窗体左上角的位置
2. Add the following code to the *.cpp file
void 类名::mousePressEvent(QMouseEvent *event){
mouse_pos=event->globalPos(); //获取鼠标点击时的绝对位置
window_pos = this->pos(); //获取窗体的位置
diff_pos=mouse_pos-window_pos;//计算相对位移
}
void 类名::mouseMoveEvent(QMouseEvent *event){
QPoint pos = event->globalPos(); //获取鼠标移动后的绝对位置
this->move(pos-diff_pos); //设置窗体移动到的位置
}