Qt窗口无标题栏拖动放大

在这里插入图片描述

.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
//#include <qt_windows.h>
//#include <Windowsx.h>
#include <QMouseEvent>

class Widget : public QWidget
{
    Q_OBJECT

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

protected:
    //bool nativeEvent(const QByteArray &eventType, void *message, long *result);
private:
    void mouseMoveEvent(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *);
    void mousePressEvent(QMouseEvent *event);
    bool  mouseLeftPressChangeSize;   //鼠标左键按下
    bool  mouseLeftPressMove;   //鼠标左键按下
    QPoint pressPoint;
    int Temp_Flag;
};
#endif // WIDGET_H

.cpp

#include "widget.h"
#include "QDebug"

#include <QPushButton>
#include <QApplication>
#include <QGraphicsDropShadowEffect>
#include <QLayout>

int g_minW = 50;
int g_minH = 50;
int g_borderRadius=20;
Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    this->setMouseTracking(true);
    // 背景透明
    setAttribute(Qt::WA_TranslucentBackground, true);
    // 设置无边框
    setWindowFlags(windowFlags() |Qt::FramelessWindowHint);
    QWidget* widget = new QWidget(this);
    widget->setObjectName("widget");
    widget->setStyleSheet("QWidget#widget{background-color:white;border-radius:5px;}");
    QHBoxLayout* hbox = new QHBoxLayout(this);
    hbox->addWidget(widget);
    this->setLayout(hbox);

    QGraphicsDropShadowEffect* shadow = new QGraphicsDropShadowEffect(widget);
    shadow->setColor(Qt::gray);
    shadow->setOffset(0, 0);
    shadow->setBlurRadius(g_borderRadius);
    widget->setGraphicsEffect(shadow);
    resize(500,500);

    mouseLeftPressChangeSize = false;
    mouseLeftPressMove = false;
}

Widget::~Widget()
{
}


void Widget::mouseMoveEvent(QMouseEvent * event)
{
    QRect rectThis = this->geometry();
    QPoint currentPoint = QCursor::pos();//获取鼠标的绝对位置
    int Temp_x = currentPoint.x() - rectThis.x();
    int Temp_y = currentPoint.y() - rectThis.y();
    if( mouseLeftPressChangeSize == false ){
        int m_border = 5;
        this->setCursor(Qt::ArrowCursor);
        int Temp_FlagRight = 0,Temp_FlagBotton = 0;

        //下
        if( qAbs( Temp_y - this->height() ) < m_border ){
            Temp_FlagBotton = 1;
            Temp_Flag = 2;
            this->setCursor(Qt::SizeVerCursor);
        }
        //右
        if( qAbs( Temp_x - this->width() ) < m_border ){
            Temp_FlagRight = 1;
            Temp_Flag = 4;
            this->setCursor(Qt::SizeHorCursor);
        }
        //右下
        if( Temp_FlagBotton && Temp_FlagRight ){
            this->setCursor(Qt::SizeFDiagCursor);
            Temp_Flag = 8;
        }
    }
    else{
        if ((event->buttons() == Qt::LeftButton) )
        {
            if(Temp_x < g_minW || Temp_y < g_minH)
               return;
            switch (Temp_Flag) {
            case 2://下
                move(rectThis.x(),rectThis.y());
                this->setFixedHeight( Temp_y );
                break;
            case 4://右
                move(rectThis.x(),rectThis.y());
                this->setFixedWidth( Temp_x );
                break;
            case 8://右下
                move(rectThis.x(),rectThis.y());
                this->setFixedSize( Temp_x ,Temp_y );
                break;
            default:
                break;
            }
        }
    }

    if (mouseLeftPressMove && (event->buttons() == Qt::LeftButton))
    {
        move(event->globalPos() - pressPoint);
    }
}

void Widget::mouseReleaseEvent(QMouseEvent *)
{
   mouseLeftPressMove = false;
   mouseLeftPressChangeSize = false;
}

void Widget::mousePressEvent(QMouseEvent * event)
{
    QRect rectThis = this->geometry(); //界面Rect
    QPoint currentPoint = QCursor::pos();//获取鼠标的绝对位置

    int m_border = 5;//焦点范围
    int Temp_FlagRight = 0,Temp_FlagBotton = 0;

    if (event->buttons() == Qt::LeftButton) {
        //下
        if( qAbs( currentPoint.y() - rectThis.y() - this->height() ) < m_border ){
            Temp_FlagBotton = 1;
            mouseLeftPressChangeSize = true;
        }
        //右
        if( qAbs( currentPoint.x() - rectThis.x() - this->width() ) < m_border ){
            Temp_FlagRight = 1;
            mouseLeftPressChangeSize = true;
        }
        //右下
        if( Temp_FlagBotton && Temp_FlagRight ){
            mouseLeftPressChangeSize = true;
        }

        if(!mouseLeftPressChangeSize &&
                currentPoint.x() > this->x() && currentPoint.x() < this->x() + width() &&
                currentPoint.y() > this->y() && currentPoint.y() < this->y() + height()){
            mouseLeftPressMove = true;
            pressPoint = event->globalPos() - pos();
        }
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值