个人博客地址:http://wtqdreamer.top
新建窗体
新建两个窗体,一个主窗体QMainWindow一个QWidget窗体,之后在主窗体中添加隐藏标题栏以及关闭按钮。
//设置标题名
this->setWindowTitle("移动无边框窗体");
//去掉标题栏
this->setWindowFlags(Qt::FramelessWindowHint);
//代码添加一个关闭按钮当然也可以ui界面添加
btClose = new QPushButton(this);
//按钮显示值
btClose->setText("关闭");
//按钮显示位置(x,y,width,heigth)
btClose->setGeometry(QRect(170, 50, 100, 25));
//按钮点击事件
connect(btClose, SIGNAL(clicked()), this, SLOT(close()));
之后因为标题栏已经隐藏所以想要移动窗体就无法通过拖动标题栏来实现
通过重写鼠标事件来实现窗体拖动,在move_window.h中加入
#include<QMouseEvent>
#include<QPushButton>
//定义鼠标的三种状态
protected:
//鼠标按下
void mousePressEvent(QMouseEvent* e);
//鼠标移动
void mouseMoveEvent(QMouseEvent* e);
//鼠标释放
void mouseReleaseEvent(QMouseEvent* e);
//定义QPint对象
private:
QPoint last;
move_window.c中重写鼠标事件