#ifndef FRAMELESSWINDOW_H
#define FRAMELESSWINDOW_H
#include <QtGui/QWidget>
#include "ui_framelesswindow.h"
class QMouseEvent;
class framelessWindow : public QWidget
{
Q_OBJECT
public:
framelessWindow(QWidget *parent = 0, Qt::WFlags flags = 0);
~framelessWindow();
private:
Ui::framelessWindowClass ui;
void mousePressEvent(QMouseEvent *event) override;
void mouseMoveEvent(QMouseEvent *event) override;
void mouseReleaseEvent(QMouseEvent *event) override;
bool mIsPressed;
QPoint mVectorDiff;
};
#endif // FRAMELESSWINDOW_H
#include "framelesswindow.h"
#include <QMouseEvent>
framelessWindow::framelessWindow(QWidget *parent, Qt::WFlags flags)
: QWidget(parent, flags)
, mIsPressed(false)
{
ui.setupUi(this);
setWindowFlags(Qt::FramelessWindowHint | Qt::WindowMinimizeButtonHint);
}
framelessWindow::~framelessWindow()
{
}
void framelessWindow::mousePressEvent(QMouseEvent *event)
{
mIsPressed = true;
// event->globalPos():向量,从屏幕左上到鼠标
// this->pos():向量,从屏幕左上到程序左上
// mVectorDiff:向量差
mVectorDiff = event->globalPos() - this->pos();
}
void framelessWindow::mouseMoveEvent(QMouseEvent *event)
{
if (mIsPressed)
{
// 鼠标在程序中的坐标不变,即向量差不变
QPoint targetPoint = event->globalPos() - mVectorDiff;
move(targetPoint);
}
}
void framelessWindow::mouseReleaseEvent(QMouseEvent *event)
{
mIsPressed = false;
}