QT隐藏系统标题栏实现窗体拖拽移动
程序员文章站
2022-07-13 23:18:10
...
LoginUI.h
#pragma once
#include <QDialog>
#include "ui_LoginUI.h"
class LoginUI : public QDialog
{
Q_OBJECT
public:
LoginUI(QWidget *parent = Q_NULLPTR);
~LoginUI();
//界面相关
public:
void InitUI();
protected:
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
private:
Ui::LoginUI * ui;
bool m_move;
QPoint m_startPoint;
QPoint m_windowPoint;
};
LoginUI.cpp
#include "LoginUI.h"
LoginUI::LoginUI(QWidget *parent)
: QDialog(parent)
, ui(new Ui::LoginUI)
, m_move(false)
{
ui->setupUi(this);
InitUI();
}
LoginUI::~LoginUI()
{
}
void LoginUI::InitUI()
{
setWindowFlags(Qt::Window | Qt::FramelessWindowHint
| Qt::WindowSystemMenuHint | Qt::WindowMinimizeButtonHint | Qt::WindowMaximizeButtonHint);
}
void LoginUI::mousePressEvent(QMouseEvent *event)
{
//当鼠标左键点击时.
if (event->button() == Qt::LeftButton)
{
m_move = true;
//记录鼠标的世界坐标.
m_startPoint = event->globalPos();
//记录窗体的世界坐标.
m_windowPoint = this->frameGeometry().topLeft();
}
}
void LoginUI::mouseMoveEvent(QMouseEvent *event)
{
if ((event->buttons()) & Qt::LeftButton && m_move)
{
//移动中的鼠标位置相对于初始位置的相对位置.
QPoint relativePos = event->globalPos() - m_startPoint;
//然后移动窗体即可.
this->move(m_windowPoint + relativePos);
}
}
void LoginUI::mouseReleaseEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
{
//改变移动状态.
m_move = false;
}
}
推荐阅读