• Qt窗口无标题栏拖动放大


    在这里插入图片描述

    .h

    #ifndef WIDGET_H
    #define WIDGET_H
    
    #include 
    //#include 
    //#include 
    #include 
    
    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
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    .cpp

    #include "widget.h"
    #include "QDebug"
    
    #include 
    #include 
    #include 
    #include 
    
    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();
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
  • 相关阅读:
    用ChatGPT自动生成流程图
    maven运行spring boot项目
    CANoe-设置CAN信号时提示没有信号驱动可用的问题
    两直线垂直,斜率乘积为-1的证明
    社群运营怎么做,有哪些互动玩法?
    VMware虚拟机找不到*.vmdk文件
    多线程 Leetcode 打印零与奇偶数
    亥姆霍兹线圈主要用途有哪些
    学习笔记——BSGS
    mapboxgl 中插值表达式的应用场景
  • 原文地址:https://blog.csdn.net/kchmmd/article/details/134266667