首先设置无边框
在这里一定要注意的是函数名称是setWindowFlags而不是setWindowFlag,
一字之差可能让你头疼半天为什么边框还在,没效果
1 //设置无边框
2 this->setWindowFlags(Qt::FramelessWindowHint);
然后是拖动功能
.h文件设置
1 #include <QMouseEvent>
2
3 protected:
4 void mouseMoveEvent(QMouseEvent *event);
5 void mousePressEvent(QMouseEvent *event);
6 void mouseReleaseEvent(QMouseEvent *event);
7 private:
8 QPoint mousePoint;
9 bool mouse_press;
.cpp文件设置
1 void Softdog::mousePressEvent(QMouseEvent *event)
2 {
3
4 if( (event->button() == Qt::LeftButton) ){
5 mouse_press = true;
6 mousePoint = event->globalPos() - this->pos();
7 // event->accept();
8 }
9 else if(event->button() == Qt::RightButton){
10 //如果是右键
11 this->close();
12
13 }
14 }
15 void Softdog::mouseMoveEvent(QMouseEvent *event)
16 {
17
18
19 // if(event->buttons() == Qt::LeftButton){ //如果这里写这行代码,拖动会有点问题
20 if(mouse_press){
21 move(event->globalPos() - mousePoint);
22 // event->accept();
23 }
24 }
25 void Softdog::mouseReleaseEvent(QMouseEvent *event)
26 {
27 mouse_press = false;
28 }