#include
#include
#include
class FramelessWindow : public QWidget
{
public:
FramelessWindow()
{
// 设置无边框
setWindowFlags(Qt::FramelessWindowHint);
setMouseTracking(true);
}
protected:
void mousePressEvent(QMouseEvent* event) override
{
if (event->button() == Qt::LeftButton)
{
// 记录鼠标点击的位置
m_dragPosition = event->globalPos() - frameGeometry().topLeft();
event->accept();
}
}
void mouseMoveEvent(QMouseEvent* event) override
{
if (event->buttons() & Qt::LeftButton)
{
// 通过拖动位置来移动窗口
move(event->globalPos() - m_dragPosition);
event->accept();
}
}
private:
QPoint m_dragPosition;
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
FramelessWindow w;
w.resize(400, 300);
w.show();
return a.exec();
}