qt 鼠标拖动窗口放大缩小

// 鼠标拖动 具体实现
void mouseMoveEvent(QMouseEvent * pEvent) { if (pEvent->buttons() & Qt::LeftButton) { if (resizeDir == nodir) {//移动窗口 QPoint newPos = pEvent->globalPos(); move(newPos - m_PressedPos); emit signal_drawWidgetPositionChange(this->pos().x(), this->pos().y()); } else { //ptop,pbottom,pleft,pright; //窗口上下左右的值 int ptop = frameGeometry().top(); int pbottom = frameGeometry().bottom(); int pleft = frameGeometry().left(); int pright = frameGeometry().right(); if(resizeDir & top) { //检测更改尺寸方向中包含的上下左右分量 if(height() == minimumHeight()) { ptop = min(pEvent->globalY(),ptop); } else if(height() == maximumHeight()) { ptop = max(pEvent->globalY(),ptop); } else { ptop = pEvent->globalY(); } } else if(resizeDir & bottom) { if(height() == minimumHeight()) { pbottom = max(pEvent->globalY(),ptop); } else if(height() == maximumHeight()) { pbottom = min(pEvent->globalY(),ptop); } else { pbottom = pEvent->globalY(); } } if(resizeDir & left) { //检测左右分量 if(width() == minimumWidth()) { pleft = min(pEvent->globalX(),pleft); } else if(width() == maximumWidth()) { pleft = max(pEvent->globalX(),pleft); } else { pleft = pEvent->globalX(); } } else if(resizeDir & right) { if(width() == minimumWidth()) { pright = max(pEvent->globalX(),pright); } else if(width() == maximumWidth()) { pright = min(pEvent->globalX(),pright); } else { pright = pEvent->globalX(); } } //setGeometry(QRect(QPoint(pleft,ptop),QPoint(pright, pbottom))); // 限制大小 if (m_iMaxWindowWidth <= pright - pleft) { if (m_iMaxWindowHeight <= pbottom - ptop) { setGeometry(QRect(QPoint(pleft,ptop),QPoint(pleft + m_iMaxWindowWidth, ptop + m_iMaxWindowHeight))); }else { setGeometry(QRect(QPoint(pleft,ptop),QPoint(pleft + m_iMaxWindowWidth, pbottom))); } }else { if (m_iMaxWindowHeight <= pbottom - ptop) { setGeometry(QRect(QPoint(pleft,ptop),QPoint(pright, ptop + m_iMaxWindowHeight))); }else { setGeometry(QRect(QPoint(pleft,ptop),QPoint(pright, pbottom))); } } } } else { findWidgetEdge(); }//当不拖动窗口、不改变窗口大小尺寸的时候 检测鼠标边缘 }

 

// 计算窗口边缘

void findWidgetEdge()
{
    int diffLeft = abs(cursor().pos().x() - frameGeometry().left());      //计算鼠标距离窗口上下左右有多少距离
    int diffRight = abs(cursor().pos().x() - frameGeometry().right());
    int diffTop = abs(cursor().pos().y() - frameGeometry().top());
    int diffBottom = abs(cursor().pos().y() - frameGeometry().bottom());

    QCursor tempCursor;                                    //获得当前鼠标样式
    tempCursor = cursor();

    if(diffTop < m_iEdgeMargin)
    {
        //根据 边缘距离 分类改变尺寸的方向
        if(diffLeft < m_iEdgeMargin)
        {
            resizeDir = topLeft;
            tempCursor.setShape(Qt::SizeFDiagCursor);
        }
        else if(diffRight < m_iEdgeMargin)
        {
            resizeDir = topRight;
            tempCursor.setShape(Qt::SizeBDiagCursor);
        }
        else
        {
            resizeDir = top;
            tempCursor.setShape(Qt::SizeVerCursor);
        }
    }
    else if(diffBottom < m_iEdgeMargin)
    {
        if(diffLeft < m_iEdgeMargin)
        {
            resizeDir = bottomLeft;
            tempCursor.setShape(Qt::SizeBDiagCursor);
        }
        else if(diffRight < m_iEdgeMargin)
        {
            resizeDir = bottomRight;
            tempCursor.setShape(Qt::SizeFDiagCursor);
        }
        else
        {
            resizeDir = bottom;
            tempCursor.setShape(Qt::SizeVerCursor);
        }
    }
    else if(diffLeft < m_iEdgeMargin)
    {
        resizeDir = left;
        tempCursor.setShape(Qt::SizeHorCursor);
    }
    else if(diffRight < m_iEdgeMargin)
    {
        resizeDir = right;
        tempCursor.setShape(Qt::SizeHorCursor);
    }
    else
    {
        resizeDir = nodir;
        tempCursor.setShape(Qt::ArrowCursor);
    }

    setCursor(tempCursor);                    //重新设置鼠标,主要是改样式
}

 

void mouseReleaseEvent(QMouseEvent *)
{
    if(resizeDir != nodir)
    {         //还原鼠标样式
        findWidgetEdge();
    }
//    this->setCursor(Qt::ArrowCursor);
}

 

void mousePressEvent(QMouseEvent * pEvent)
{
    if (Qt::LeftButton == pEvent->button())
    {
        this->setCursor(Qt::PointingHandCursor);
        m_PressedPos = pEvent->globalPos() - this->pos();
        m_dragPosition = pEvent->globalPos() - frameGeometry().topLeft();  //获得鼠标按键位置相对窗口左上面的位置
    }
}

 

posted @ 2017-12-22 15:53  风来风往风伤(TiAmo)  阅读(3928)  评论(1编辑  收藏  举报