头文件
public:
Widget(QWidget *parent = nullptr);
~Widget();
QPoint mousePoint;
bool m_mousePressed;
void mouseMoveEvent(QMouseEvent *event);//鼠标按下事件
void mousePressEvent(QMouseEvent *event);//鼠标释放事件
void mouseReleaseEvent(QMouseEvent *event);//鼠标松开事件 源文件
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
this->setWindowFlags(this->windowFlags() | Qt::FramelessWindowHint);
m_mousePressed = false;
}
Widget::~Widget()
{
delete ui;
}
void Widget::mouseMoveEvent(QMouseEvent *event)
{
if(m_mousePressed && (event->buttons() && Qt::LeftButton)){
this->move(event->globalPos()- mousePoint);
event->accept(); //accept ()函数用来告诉Qt,事件处理函数“接收”了这个事件,不要再传递;
}
}
void Widget::mousePressEvent(QMouseEvent *event)
{
if(event->button()==Qt::LeftButton)
{
m_mousePressed = true;
mousePoint = event->globalPos() - this->pos();
event->accept();
}
}
void Widget::mouseReleaseEvent(QMouseEvent *event)
{
m_mousePressed = false;
}