zoukankan      html  css  js  c++  java
  • 无边框窗口拖动代码

    无边框窗口拖动代码

    protected:
        // 用于鼠标拖动窗口的鼠标事件操作
        void mousePressEvent(QMouseEvent * event);
        void mouseMoveEvent(QMouseEvent * event);
        void mouseReleaseEvent(QMouseEvent * event);

    void QDlgLogin::mousePressEvent(QMouseEvent *event)
    { // 鼠标按键被按下
        // ( event->button() == Qt::LeftButton ) 被按下的按键是左键
        if ( event->button() == Qt::LeftButton ) {
            m_moving = true;
            /*
             * 记录下鼠标相对于窗口的位置
             * event->globalPos(); // 鼠标按下时, 鼠标相对于整个屏幕位置
             * pos(); // 鼠标按下时, 窗口相对于整个屏幕位置
             */
            m_lastPos = event->globalPos() - pos();
            qDebug() << QString::fromUtf8("被点击时按下键是左键, 标记为开始移动");
        }
        return QDialog::mousePressEvent(event);
    }
    
    void QDlgLogin::mouseMoveEvent(QMouseEvent *event)
    { // 鼠标按下左键移动
        /*
         * ( event->buttons() == Qt::LeftButton ) 已经处在按下状态的是左键
         * 鼠标移动事件需要移动窗口, 窗口移动到哪里呢? 就是要获取鼠标移动中, 窗口在整个屏幕的坐标, 然后move到这个坐标, 怎么获取坐标?
         * 通过事件 event->globalPos() 知道鼠标坐标, 鼠标坐标减去鼠标相对于窗口位置, 就是窗口在整个屏幕的坐标
         */
        if ( m_moving
             && ( event->buttons() == Qt::LeftButton )
             && ( ( event->globalPos() - m_lastPos ).manhattanLength() > QApplication::startDragDistance() ) ) {
            move(event->globalPos() - m_lastPos);
            m_lastPos = event->globalPos() - pos();
            qDebug() << QString::fromUtf8("被移动时按下键是左键, 标记为窗口移动");
        }
        return QDialog::mouseMoveEvent(event);
    }
    
    void QDlgLogin::mouseReleaseEvent(QMouseEvent *event)
    { // 鼠标按键释放
        Q_UNUSED(event)
    
        // ( event->button() == Qt::LeftButton ) 被释放的按键是左键
        if ( event->button() == Qt::LeftButton ) {
            m_moving = false; // 停止移动
            qDebug() << QString::fromUtf8("被释放的按键是左键, 标记为停止移动");
        }
        return QDialog::mouseReleaseEvent(event);
    }

    ================ End

     
  • 相关阅读:
    Redis分布式锁的正确实现方式(Java版)转载
    kali 安装 Burpsuite Pro v2020.8 破解
    DVWA On KALI
    Metasploit 体系结构
    Xmodem、Ymodem、Zmodem
    Metasploit psnuffle
    Metasploit通过ssh暴力破解
    使用ms17_010渗透win7
    Metasploit快速入门(二)
    Metasploit 快速入门(一)
  • 原文地址:https://www.cnblogs.com/lsgxeva/p/12313628.html
Copyright © 2011-2022 走看看