QT_无边框窗口拖动缩放

Posted 细语细语

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了QT_无边框窗口拖动缩放相关的知识,希望对你有一定的参考价值。

    public:
        int countFlag(QPoint p, int row);
        void setCursorType(int flag);
        int countRow(QPoint p);

    protected:
        void mousePressEvent(QMouseEvent *event);
        void mouseReleaseEvent(QMouseEvent *event);
        void mouseDoubleClickEvent(QMouseEvent *event);
        void mouseMoveEvent(QMouseEvent *event);
    private:
        bool _isleftpressed;
        int _curpos;
        QPoint _plast;
//构造
this->setMouseTracking(true);
_isleftpressed = false;
_curpos = 0;//标记鼠标左击时的位置
this->setMinimumSize(600, 400);//设置最小尺寸
   void my_window::mousePressEvent(QMouseEvent *event)
    {
        if (event->button() == Qt::LeftButton)
        {
            this->_isleftpressed = true;
            QPoint temp = event->globalPos();
            _plast = temp;
            _curpos = countFlag(event->pos(), countRow(event->pos()));
            event->ignore();
        }
    }

    void my_window::mouseReleaseEvent(QMouseEvent *event)
    {
        if (_isleftpressed)
            _isleftpressed = false;
        setCursor(Qt::ArrowCursor);
        event->ignore();
    }

    void my_window::mouseDoubleClickEvent(QMouseEvent *event)
    {
        if (event->button() == Qt::LeftButton)
        {
            if (windowState() != Qt::WindowFullScreen)
                setWindowState(Qt::WindowFullScreen);
            else setWindowState(Qt::WindowNoState);//恢复正常模式
        }
        event->ignore();
    }

    void my_window::mouseMoveEvent(QMouseEvent *event)//鼠标移动事件
    {

        int poss = countFlag(event->pos(), countRow(event->pos()));
        setCursorType(poss);
        if (_isleftpressed)//是否左击
        {
            QPoint ptemp = event->globalPos();
            ptemp = ptemp - _plast;
            if (_curpos == 22)//移动窗口
            {
                ptemp = ptemp + pos();
                move(ptemp);
            }
            else
            {
                QRect wid = geometry();
                switch (_curpos)//改变窗口的大小
                {

                case 11:wid.setTopLeft(wid.topLeft() + ptemp); break;//左上角
                case 13:wid.setTopRight(wid.topRight() + ptemp); break;//右上角
                case 31:wid.setBottomLeft(wid.bottomLeft() + ptemp); break;//左下角
                case 33:wid.setBottomRight(wid.bottomRight() + ptemp); break;//右下角
                case 12:wid.setTop(wid.top() + ptemp.y()); break;//中上角
                case 21:wid.setLeft(wid.left() + ptemp.x()); break;//中左角
                case 23:wid.setRight(wid.right() + ptemp.x()); break;//中右角
                case 32:wid.setBottom(wid.bottom() + ptemp.y()); break;//中下角
                }
                setGeometry(wid);
            }

            _plast = event->globalPos();//更新位置
        }
        event->ignore();
    }



    int my_window::countFlag(QPoint p, int row)//计算鼠标在哪一列和哪一行
    {
        if (p.y()<MARGIN)
            return 10 + row;
        else if (p.y()>this->height() - MARGIN)
            return 30 + row;
        else
            return 20 + row;
    }

    void my_window::setCursorType(int flag)//根据鼠标所在位置改变鼠标指针形状
    {
        Qt::CursorShape cursor;
        switch (flag)
        {
        case 11:
        case 33:
            cursor = Qt::SizeFDiagCursor; break;
        case 13:
        case 31:
            cursor = Qt::SizeBDiagCursor; break;
        case 21:
        case 23:
            cursor = Qt::SizeHorCursor; break;
        case 12:
        case 32:
            cursor = Qt::SizeVerCursor; break;
        case 22:
            //cursor = Qt::OpenHandCursor; break;
            cursor = Qt::ArrowCursor; break;
        default:
            cursor = Qt::ArrowCursor; break;
            break;

        }
        setCursor(cursor);
    }

    int my_window::countRow(QPoint p)//计算在哪一列
    {
        return (p.x()<MARGIN) ? 1 : (p.x()>(this->width() - MARGIN) ? 3 : 2);
    }

技术分享

以上是关于QT_无边框窗口拖动缩放的主要内容,如果未能解决你的问题,请参考以下文章

qt里面怎么设置窗口大小改变不触发update

Qt 无标题无边框程序的拖动和改变大小

Qt自定义界面边框后,移动鼠标拖动界面,界面会拖到任务栏以下。。。如何解决?

在 Qt 中使用无边框窗口的 Aero Snap

MFC中无边框窗口的拖动 怎么添加WM_NCLBUTTONDOWN消息映射

jqgrid 在拖动列时 能不能不显示 列的边框 虚线?