QT无边框窗体的缩放和移动

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>

namespace Ui {
class Dialog;
}

enum SizeChangeMode{
    Ver_Top,//向上
    Ver_Bottom,//向下
    Hor_Left,//向左
    Hor_Right,//向右
    FDiag_LT,//左上
    FDiag_RB,//右下
    BDiag_LB,//左下
    BDiag_RT,//右上
    Arrow//正中
};

class Dialog : public QDialog
{
    Q_OBJECT

public:
    explicit Dialog(QWidget *parent = 0);
    ~Dialog();

private:
    virtual void mouseMoveEvent(QMouseEvent *event)Q_DECL_OVERRIDE;
    virtual void resizeEvent(QResizeEvent *event)Q_DECL_OVERRIDE;
    virtual void mousePressEvent(QMouseEvent *event)Q_DECL_OVERRIDE;
    virtual void mouseReleaseEvent(QMouseEvent *event)Q_DECL_OVERRIDE;

private:
    SizeChangeMode chageMouseCursor(QPoint point);
private:
    Ui::Dialog *ui;
    bool m_bLeftBtnPress;
    SizeChangeMode m_cursorType;//鼠标状态
    QPoint m_MousePressPos; //鼠标按下位置
    QPoint m_MouseMovePos;  //鼠标移动位置
};

#endif // DIALOG_H

#include "dialog.h"
#include "ui_dialog.h"
#include <QMouseEvent>
#include <QResizeEvent>
#include <QPoint>
#include <QDebug>
#include <QCursor>

#define BorderSize 5

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    m_bLeftBtnPress(false),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);

    this->setWindowFlags(Qt::FramelessWindowHint);
    this->setMouseTracking(true);
    ui->widget->setAttribute(Qt::WA_TransparentForMouseEvents,true);
}

Dialog::~Dialog()
{
    delete ui;
}

void Dialog::mouseMoveEvent(QMouseEvent *event)
{
    if(m_bLeftBtnPress){
        m_MouseMovePos = event->globalPos();
        int move_width_range = m_MouseMovePos.rx() - m_MousePressPos.rx();
        int move_height_range = m_MouseMovePos.ry() - m_MousePressPos.ry();
        switch (m_cursorType) {
        case Ver_Top:{
            QRect rect = this->geometry();
            rect.setTop(rect.top() + move_height_range);
            if((rect.bottom() - rect.top()) >= 100)
                this->setGeometry(rect);
            m_MousePressPos = m_MouseMovePos;
            break;
        }
        case Ver_Bottom:{
            QRect rect = this->geometry();
            rect.setBottom(rect.bottom() + move_height_range);
            if((rect.bottom() - rect.top()) >= 100)
                this->setGeometry(rect);
            m_MousePressPos = m_MouseMovePos;
            break;
        }
        case Hor_Left:{
            QRect rect = this->geometry();
            rect.setLeft(rect.left() + move_width_range);
            if((rect.right() - rect.left()) >= 100)
                this->setGeometry(rect);
            m_MousePressPos = m_MouseMovePos;
            break;
        }
        case Hor_Right:{
            QRect rect = this->geometry();
            rect.setRight(rect.right() + move_width_range);
            if((rect.right() - rect.left()) >= 100)
                this->setGeometry(rect);
            m_MousePressPos = m_MouseMovePos;
            break;
        }
        case FDiag_LT:{
            QRect rect = this->geometry();
            rect.setTop(rect.top() + move_height_range);
            rect.setLeft(rect.left() + move_width_range);
            if((rect.bottom() - rect.top()) >= 100 && (rect.right() - rect.left()) >= 100)
                this->setGeometry(rect);
            m_MousePressPos = m_MouseMovePos;
            break;
        }
        case FDiag_RB:{
            QRect rect = this->geometry();
            rect.setBottom(rect.bottom() + move_height_range);
            rect.setRight(rect.right() + move_width_range);
            if((rect.bottom() - rect.top()) >= 100 && (rect.right() - rect.left()) >= 100)
                this->setGeometry(rect);
            m_MousePressPos = m_MouseMovePos;
            break;
        }
        case BDiag_LB:{
            QRect rect = this->geometry();
            rect.setBottom(rect.bottom() + move_height_range);
            rect.setLeft(rect.left() + move_width_range);
            if((rect.bottom() - rect.top()) >= 100 && (rect.right() - rect.left()) >= 100)
                this->setGeometry(rect);
            m_MousePressPos = m_MouseMovePos;
            break;
        }
        case BDiag_RT:{
            QRect rect = this->geometry();
            rect.setTop(rect.top() + move_height_range);
            rect.setRight(rect.right() + move_width_range);
            if((rect.bottom() - rect.top()) >= 100 && (rect.right() - rect.left()) >= 100)
                this->setGeometry(rect);
            m_MousePressPos = m_MouseMovePos;
            break;
        }
        case Arrow:{
            m_MouseMovePos = event->globalPos();
            this->move(this->pos() + m_MouseMovePos - m_MousePressPos);
            m_MousePressPos = m_MouseMovePos;
            break;
        }
        default:
            break;
        }
    }
    else{
        m_cursorType = this->chageMouseCursor(event->pos());
    }
    event->accept();
}

void Dialog::resizeEvent(QResizeEvent *event)
{
    ui->widget->setGeometry(1,1,this->width() - 2,this->height() - 2);
    event->accept();
}

void Dialog::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton){
        m_bLeftBtnPress = true;
        m_MousePressPos = event->globalPos();
    }
}

void Dialog::mouseReleaseEvent(QMouseEvent *event)
{
    m_bLeftBtnPress = false;
}

SizeChangeMode Dialog::chageMouseCursor(QPoint point)
{
    //judge the mouse point if in custom border change the mouse cursor

    /* Ver_Top */
    if(point.rx() >= BorderSize
            && point.rx() <= (this->width() - 2*BorderSize)
            && point.ry() <= BorderSize){
        this->setCursor(Qt::SizeVerCursor);
        return Ver_Top;
    }

    /* Ver_Bottom */
    if(point.rx() >= BorderSize
            && point.rx() <= (this->width() - 2*BorderSize)
            && point.ry() >= (this->height() - 2*BorderSize)){
        this->setCursor(Qt::SizeVerCursor);
        return Ver_Bottom;
    }

    /* Hor_Left */
    if(point.rx() <= BorderSize
            && point.ry() >= BorderSize
            && point.ry() <= (this->height() - 2*BorderSize)){
        this->setCursor(Qt::SizeHorCursor);
        return Hor_Left;
    }

    /* Hor_Right */
    if(point.rx() >= (this->width() - 2*BorderSize)
            && point.ry() >= BorderSize
            && point.ry() <= (this->height() - 2*BorderSize)){
        this->setCursor(Qt::SizeHorCursor);
        return Hor_Right;
    }

    /* FDiag_LT */
    if(point.rx() <= BorderSize
            && point.ry() <= BorderSize){
        this->setCursor(Qt::SizeFDiagCursor);
        return FDiag_LT;
    }

    /* FDiag_RB */
    if(point.rx() >= (this->width() - 2*BorderSize)
            && point.ry() >= (this->height() - 2*BorderSize)){
        this->setCursor(Qt::SizeFDiagCursor);
        return FDiag_RB;
    }

    /* BDiag_LB */
    if(point.rx() <= BorderSize
            && point.ry() >= (this->height() - 2*BorderSize)){
        this->setCursor(Qt::SizeBDiagCursor);
        return BDiag_LB;
    }

    /* BDiag_RT */
    if(point.rx() >= (this->width() - 2*BorderSize)
            && point.ry() <= BorderSize){
        this->setCursor(Qt::SizeBDiagCursor);
        return BDiag_RT;
    }

    /* Arrow */
    this->setCursor(Qt::ArrowCursor);
    return Arrow;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值