Qt实现窗口拉伸

我们在日常开发的时候,几乎是很大的可能不使用系统自带的边框,但是我们去掉的话就没有窗口的拉伸效果,在网上找了几个demo都有bug,而且感觉写的很复杂,所以我重写了一个,如果有什么bug希望大家留言告知我。

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QMouseEvent>

enum ClickType
{
    CLICK_NULL,
    CLICK_TOP,
    CLICK_BOTTOM,
    CLICK_LEFT,
    CLICK_RIGHT,
    CLICK_BOTTOM_RIGHT,
    CLICK_TOP_RIGHT,
    CLICK_TOP_LETT,
    CLICK_BOTTOM_LETT,
};

namespace Ui {class Widget;}

class Widget : public QWidget
{
    Q_OBJECT

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

protected:
    void mouseMoveEvent(QMouseEvent *ev);
    void mousePressEvent(QMouseEvent *ev);
    void mouseReleaseEvent(QMouseEvent *ev);

private slots:
    void on_pushButton_clicked();

private:
    Ui::Widget *ui;
    ClickType m_clickType = CLICK_NULL;
    bool m_bCanResize = false;
    bool m_bResizeIng = false;

    QSize  m_oldSize;
    QPoint m_globalPoint;
};

#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"
//#include <QDebug>

#define MOUSE_GAP 10

Widget::Widget(QWidget *parent) :QWidget(parent),ui(new Ui::Widget)
{
    ui->setupUi(this);

    this->setMouseTracking(true);//开启鼠标追踪
    this->setWindowFlags(Qt::X11BypassWindowManagerHint | Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint);
    this->setMinimumSize(100,100);

    m_oldSize = this->size();
    m_globalPoint = this->pos();
}

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


void Widget::mouseMoveEvent(QMouseEvent *ev)
{
        static QPoint rightTop;
    static QPoint leftBottom;
    static QPoint leftTop;
    static QPoint rightBottom;

    if(!m_bResizeIng)
    {
        //右下角
        if(ev->x() > this->width() - MOUSE_GAP && ev->y() > this->height() - MOUSE_GAP)
        {
            //qDebug() << "右下";
            this->setCursor(Qt::SizeFDiagCursor);
            m_clickType = CLICK_BOTTOM_RIGHT;
        }
        //右上
        else if(ev->x() > this->width() - MOUSE_GAP && ev->x() > MOUSE_GAP && ev->y() < MOUSE_GAP)
        {
            //qDebug() << "右上";
            this->setCursor(Qt::SizeBDiagCursor);
            m_clickType = CLICK_TOP_RIGHT;
        }
        //左上
        else if(ev->x() < MOUSE_GAP && ev->y() < MOUSE_GAP)
        {
            //qDebug() << "左上";
            this->setCursor(Qt::SizeFDiagCursor);
            m_clickType = CLICK_TOP_LETT;
        }
        //左下
        else if(ev->x() < MOUSE_GAP && ev->y() > this->height() - MOUSE_GAP)
        {
            //qDebug() << "左下";
            this->setCursor(Qt::SizeBDiagCursor);
            m_clickType = CLICK_BOTTOM_LETT;
        }
        //左
        else if(ev->x() < MOUSE_GAP)
        {
            //qDebug() << "左";
            this->setCursor(Qt::SizeHorCursor);
            m_clickType = CLICK_LEFT;
        }
        //右边
        else if(ev->x() > this->width() - MOUSE_GAP && ev->y() < this->height() - MOUSE_GAP)
        {
            //qDebug() << "右";
            this->setCursor(Qt::SizeHorCursor);
            m_clickType = CLICK_RIGHT;
        }
        //上边
        else if(ev->x() > MOUSE_GAP && ev->y() < MOUSE_GAP)
        {
            //qDebug() << "上";
            this->setCursor(Qt::SizeVerCursor);
            m_clickType = CLICK_TOP;
        }
        //下边
        else if(ev->y() > this->height() - MOUSE_GAP)
        {
            //qDebug() << "下";
            this->setCursor(Qt::SizeVerCursor);
            m_clickType = CLICK_BOTTOM;
        }
        else
        {
            //qDebug() << "其他";
            this->setCursor(Qt::ArrowCursor);
            m_clickType = CLICK_NULL;
        }

    }
    if(m_bCanResize && m_clickType != CLICK_NULL)
    {
        //qDebug() << "可以改变尺寸";
        if(m_clickType == CLICK_RIGHT)
        {
            if(m_globalPoint.x() < ev->globalPos().x() && ev->globalPos().x() > rightTop.x())
            {

                m_bResizeIng = true;
                //qDebug() << "向右拉大";
                this->resize(m_oldSize.width() + (ev->globalX() - m_globalPoint.x()),this->height());
            }
            else if(ev->globalPos().x() < m_globalPoint.x())
            {
                m_bResizeIng = true;
                //qDebug() << "向右拉小";
                this->resize(m_oldSize.width() - (m_globalPoint.x() - ev->globalX()),this->height());
            }
        }
        else if(m_clickType == CLICK_BOTTOM)
        {
            if(ev->globalY() > m_globalPoint.y() && ev->globalY() > this->y() + this->height())
            {
                //qDebug() << "向下拉大";
                m_bResizeIng = true;
                this->resize(this->width(),this->height() + ev->globalY() - m_globalPoint.y());
            }
            else if(ev->globalY() < m_globalPoint.y())
            {
                //qDebug() << "向下拉小";
                m_bResizeIng = true;
                this->resize(this->width(),this->height() - (m_globalPoint.y() - ev->globalY()));
            }
        }
        else if(m_clickType == CLICK_LEFT)
        {
            if(m_globalPoint.x() > ev->globalX() && leftTop.x() > ev->globalX())
            {
                m_bResizeIng = true;
                //qDebug() << "向左拉大";
                this->resize(m_oldSize.width() + m_globalPoint.x() - ev->globalX(),this->height());
                this->move(this->x() - (m_globalPoint.x() - ev->globalPos().x()),this->y());
            }
            else if(m_globalPoint.x() < ev->globalX())
            {
                if(this->width() != this->minimumWidth())
                {
                    m_bResizeIng = true;
                    //qDebug() << "向左拉小";
                    this->resize(m_oldSize.width() + m_globalPoint.x() - ev->globalX(),this->height());
                    this->move(rightTop.x() - this->width(),this->y());
                }
            }
        }
        else if(m_clickType == CLICK_TOP)
        {
            if(m_globalPoint.y() > ev->globalY() && ev->globalY() < this->y())
            {
                m_bResizeIng = true;
                //qDebug() << "向上拉大";

                this->resize(this->width(),this->height() + (m_globalPoint.y() - ev->globalY()));
                this->move(this->x(),this->y() - (m_globalPoint.y() - ev->globalY()));
            }
            else if(m_globalPoint.y() < ev->globalY())
            {
                m_bResizeIng = true;
                //qDebug() << "向上拉小";
                this->resize(this->width(),this->height() - (ev->globalY() - m_globalPoint.y()));
                this->move(this->x(),leftBottom.y() - this->height());
            }
        }
        else if(m_clickType == CLICK_BOTTOM_RIGHT)
        {
            //拉大
            if(ev->globalX() > m_globalPoint.x() || ev->globalY() > m_globalPoint.y())
            {
                if(ev->globalX() > rightBottom.x())
                {
                    m_bResizeIng = true;
                    this->resize(m_oldSize.width() + (ev->globalX() - m_globalPoint.x()),this->height());
                }
                if(ev->globalY() > rightBottom.y())
                {
                    m_bResizeIng = true;
                    this->resize(this->width(),this->height() + ev->globalY() - m_globalPoint.y());
                }
            }
            //缩小
            else if(ev->globalX() < m_globalPoint.x() || ev->globalY() < m_globalPoint.y())
            {
                m_bResizeIng = true;
                //qDebug() << "右下拉小";
                this->resize(m_oldSize.width() - (m_globalPoint.x() - ev->globalX()),this->height());
                this->resize(this->width(),this->height() - (m_globalPoint.y() - ev->globalY()));
            }
        }
        else if(m_clickType == CLICK_TOP_RIGHT)
        {
            if(ev->globalX() > m_globalPoint.x() || ev->globalY() < m_globalPoint.y())
            {
                //qDebug() << "右上拉大";
                if(ev->globalX() > rightTop.x())
                {
                    m_bResizeIng = true;
                    this->resize(m_oldSize.width() + (ev->globalX() - m_globalPoint.x()),this->height());
                }
                if(ev->globalY() < rightTop.y())
                {
                    m_bResizeIng = true;
                    this->resize(this->width(),this->height() + (m_globalPoint.y() - ev->globalY()));
                    this->move(leftTop.x(),this->y() - (m_globalPoint.y() - ev->globalY()));
                }

            }
            else if(ev->globalX() < m_globalPoint.x() || ev->globalY() > m_globalPoint.y())
            {
                m_bResizeIng = true;
                //qDebug() << "右上拉小";

                this->resize(this->width(),this->height() - (ev->globalY() - m_globalPoint.y()));
                this->resize(m_oldSize.width() - (m_globalPoint.x() - ev->globalX()),this->height());
                this->move(leftTop.x(),leftBottom.y() - this->height());
            }
        }
        else if(m_clickType == CLICK_TOP_LETT)
        {
            if(ev->globalX() < m_globalPoint.x() || ev->globalY() < m_globalPoint.y())
            {
                //qDebug() << "左上拉大";
                if(ev->globalX() < leftTop.x())
                {
                    m_bResizeIng = true;
                    this->resize(m_oldSize.width() + m_globalPoint.x() - ev->globalX(),this->height());
                    this->move(this->x() - (m_globalPoint.x() - ev->globalPos().x()),leftTop.y());
                }
                if(ev->globalY() < leftTop.y())
                {
                    m_bResizeIng = true;
                    this->resize(this->width(),this->height() + (m_globalPoint.y() - ev->globalY()));
                    this->move(this->x(),this->y() - (m_globalPoint.y() - ev->globalY()));
                }
            }
            else if(ev->globalX() > m_globalPoint.x() || ev->globalY() > m_globalPoint.y())
            {
                m_bResizeIng = true;
                //qDebug() << "左上拉小";
                this->resize(m_oldSize.width() + m_globalPoint.x() - ev->globalX(),this->height());
                this->move(rightTop.x() - this->width(),this->y());

                if(ev->globalY() > leftTop.y())
                {
                    //qDebug() << ev->globalY() << leftTop.y();
                    this->resize(this->width(),this->height() - (ev->globalY() - m_globalPoint.y()));
                    this->move(this->x(),leftBottom .y()- this->height());
                }
            }
        }
        else if(m_clickType == CLICK_BOTTOM_LETT)
        {
            if(ev->globalX() < m_globalPoint.x() || ev->globalY() > m_globalPoint.y())
            {
                 //qDebug() << "左下拉大";
                if(ev->globalX() < leftTop.x())
                {
                    m_bResizeIng = true;
                    this->resize(m_oldSize.width() + m_globalPoint.x() - ev->globalX(),this->height());
                    this->move(this->x() - (m_globalPoint.x() - ev->globalPos().x()),this->y());
                }
                if(ev->globalY() > leftBottom.y())
                {
                    m_bResizeIng = true;
                    this->resize(this->width(),this->height() + ev->globalY() - m_globalPoint.y());
                }
            }
            else if(ev->globalX() > m_globalPoint.x() || ev->globalY() < m_globalPoint.y())
            {
                m_bResizeIng = true;
                //qDebug() << "左下拉小";

                this->resize(this->width(),this->height() - (m_globalPoint.y() - ev->globalY()));
                this->resize(m_oldSize.width() + m_globalPoint.x() - ev->globalX(),this->height());
                this->move(rightTop.x() - this->width(),this->y());
            }
        }
    }

    leftTop = this->pos();
    leftBottom.setX(this->pos().x());leftBottom.setY(this->y() + this->height());
    rightTop.setX(this->x() + this->width());rightTop.setY(this->y());
    rightBottom.setX(this->x() + this->width());rightBottom.setY(this->y() + this->height());
    m_oldSize = this->size();
    m_globalPoint = ev->globalPos();
}

void Widget::mousePressEvent(QMouseEvent *ev)
{
    if(ev->buttons() == Qt::LeftButton && m_clickType != CLICK_NULL)
    {
        m_bCanResize = true;
    }
}

void Widget::mouseReleaseEvent(QMouseEvent *ev)
{
    this->setCursor(Qt::ArrowCursor);
    m_clickType = CLICK_NULL;
    m_bCanResize = false;
    m_bResizeIng = false;
}

void Widget::on_pushButton_clicked()
{
    QApplication::exit();
}

PS:

       1、窗口最好限定最大最小尺寸。

       2、向左拉伸的时候,窗口最右边会有抖动,但是我发现市面上支持拉伸的软件向左拉伸时都   会有这个问题并且Qt的源程序也会有这个问题,而且我打印了坐标发现点位并没有变化,所以这应该不是我们的问题。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值