qt 实现去掉标题栏之后(frameless)仍然可以拖动的对话框

movable-base-dialog.h

#ifndef MOVABLE_BASE_DLG_H
#define MOVABLE_BASE_DLG_H

#include 

class QPoint;
class QPaintEvent;
class ZWWMovableDialog :public QDialog{
    Q_OBJECT
public:
    ZWWMovableDialog(QWidget* parent);
    ~ZWWMovableDialog(){};

signals:
       void mouseButtonMove(QPoint pos);
       void signalMainWindowMove();

protected:
    virtual void mouseReleaseEvent(QMouseEvent *event) override;
    virtual void mouseMoveEvent(QMouseEvent *event) override;
    virtual void mousePressEvent(QMouseEvent *event) override;

private:
    bool            m_Drag = false;
    QPoint          m_DragPosition;
};
#endif

movable-base-dialog.cpp

#include 
#include 
#include 
#include 
#include 

#include "movable-base-dialog.h"

ZWWMovableDialog::ZWWMovableDialog(QWidget* parent)
:QDialog(parent)
{
}

void ZWWMovableDialog::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        m_Drag = true;
        m_DragPosition = event->globalPos() - this->pos();
        event->accept();
    }
    QDialog::mousePressEvent(event);
}

void ZWWMovableDialog::mouseMoveEvent(QMouseEvent *event)
{
    if (m_Drag && (event->buttons() && Qt::LeftButton)) {
        move(event->globalPos() - m_DragPosition);
        event->accept();
        emit mouseButtonMove(event->globalPos() - m_DragPosition);
        emit signalMainWindowMove();
    }
    QDialog::mouseMoveEvent(event);
}

void ZWWMovableDialog::mouseReleaseEvent(QMouseEvent *event)
{
    Q_UNUSED(event);
    m_Drag = false;
    QDialog::mouseReleaseEvent(event);
}

你可能感兴趣的:(QT,qt无边框窗口,qt窗口拖动)