qt 窗口移动【在父窗口里面移动,或者可以超过父窗口范围】

//重写鼠标移动事件
void customwidget2::mouseMoveEvent(QMouseEvent *event)
{
    QString str = "I如";
    int nlength=str.length();
    qDebug(" nlenght=%d",nlength);
    int plenght=str.toLocal8Bit().size();
    qDebug(" plenght=%d",plenght);

    if(this->isMaximized()) //果最大化,则不允许移动和拉伸
        return;

    int poss=countFlag(event->pos(),countRow(event->pos()));//计算出来鼠标在哪个区域

    if(!event->buttons())
        setCursorType(poss);//根据不同的区域设置不同的鼠标形状
    QPoint ptemp1=event->globalPos();
  /*
    if(this->geometry().contains(this->mapFromGlobal(QCursor::pos())))
    {
        this->setStyleSheet("color:#158962;background:#333333;border:1px solid red");
    }else
    {
        this->setStyleSheet("color:#158962;background:#333333;border:1px solid green");
    }
    */
    if((event->buttons() & Qt::LeftButton) && m_bPressed)//是否左击
    {
        QPoint ptemp=event->globalPos();
        ptemp=ptemp-pLast;  //鼠标移动的偏移量
        if(m_curPos==22)    //区域(2,2)表示移动窗口
        {
            ptemp=ptemp+pos();
             //move(ptemp);//控制可以移动超过父窗口

            //下面是控制只能在父窗口移动

            if(mMoveing){
                QSize size = this->size();
                QSize parentSize = this->parentWidget()->size();
                QPoint curPos = event->pos() - mMovePosition + pos();
                if(curPos.x() < 0)//left
                    curPos.setX(0);

                if(curPos.y() < 0)//top
                    curPos.setY(0);

                if( (curPos.x()+size.width()) > parentSize.width())//right
                    curPos.setX(parentSize.width() - size.width());

                if( (curPos.y()+size.height()) > parentSize.height())//bottom
                    curPos.setY(parentSize.height() - size.height());

                move(curPos);


            }

        }
        else
        {
            QRect wid=geometry();

            int minWidth = this->minimumWidth();
            int minHeight = this->minimumHeight();

            switch(m_curPos)//改变窗口的大小
            {
            case 11:
            {
                QPoint pos = wid.topLeft();

                if(wid.width() > minWidth || ptemp.x() < 0)
                    pos.rx() = pos.rx() + ptemp.x();
                if(wid.height() > minHeight || ptemp.y() < 0)
                    pos.ry() = pos.ry() + ptemp.y();

                wid.setTopLeft(pos);
                break;//左上角
            }
            case 13:
            {
                QPoint pos = wid.topRight();

                if(wid.width() > minWidth || ptemp.x() > 0)
                    pos.rx() = pos.rx() + ptemp.x();
                if(wid.height() > minHeight || ptemp.y() < 0)
                    pos.ry() = pos.ry() + ptemp.y();

                wid.setTopRight(pos);
                break;//右上角
            }
            case 31:
            {
                QPoint pos = wid.bottomLeft();

                if(wid.width() > minWidth || ptemp.x() < 0)
                    pos.rx() = pos.rx() + ptemp.x();
                if(wid.height() > minHeight || ptemp.y() > 0)
                    pos.ry() = pos.ry() + ptemp.y();

                wid.setBottomLeft(pos);
                break;//左下角
            }
            case 33:
            {
                QPoint pos = wid.bottomRight();

                if(wid.width() > minWidth || ptemp.x() > 0)
                    pos.rx() = pos.rx() + ptemp.x();
                if(wid.height() > minHeight || ptemp.y() > 0)
                    pos.ry() = pos.ry() + ptemp.y();

                wid.setBottomRight(pos);
                break;//右下角
            }
            case 12:
            {
                int topY = wid.top();
                if(wid.height() > minHeight || ptemp.y() < 0)
                    topY = topY + ptemp.y();

                wid.setTop(topY);
                break;//中上角
            }
            case 21:
            {
                int leftX = wid.left();

                if(wid.width() > minWidth || ptemp.x() < 0)
                    leftX = leftX + ptemp.x();

                wid.setLeft(leftX);
                break;//中左角
            }
            case 23:
            {
                int rightX = wid.right();

                if(wid.width() > minWidth || ptemp.x() > 0)
                    rightX = rightX + ptemp.x();

                wid.setRight(rightX);
                break;//中右角
            }
            case 32:
            {
                int botY = wid.bottom();
                if(wid.height() > minHeight || ptemp.y() > 0)
                    botY = botY + ptemp.y();

                wid.setBottom(botY);
                break;//中下角
            }
            }
            if(wid.width()<=50||wid.height()<=50)
            {
                wid.setWidth(50);
                wid.setHeight(50);
            }
            setGeometry(wid);   //设置窗口的位置
        }
        pLast=event->globalPos();//更新位置
    }
    event->ignore();

    return QWidget::mouseMoveEvent(event);
}
void customwidget2::mouseReleaseEvent(QMouseEvent *event)
{

    QApplication::restoreOverrideCursor();//恢复鼠标指针性状
    event->ignore();

    m_bPressed = false;

    mMoveing = false;

}

void customwidget2::showEvent(QShowEvent* event)
{
    setAttribute(Qt::WA_Mapped);//解决不能及时刷新的bug
    QWidget::showEvent(event);
}


int customwidget2::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 customwidget2::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::ArrowCursor;break;
    default:
        //  QApplication::restoreOverrideCursor();//恢复鼠标指针性状
        break;

    }
    setCursor(cursor);
}

void customwidget2::focusInEvent ( QFocusEvent * event )
{
    this->setStyleSheet("color:#158962;background:#333333;border:1px solid red");
    bFocus=true;

}

void customwidget2::focusOutEvent ( QFocusEvent * event )
{
    this->setStyleSheet("color:#158962;background:#333333;border:1px ");
    bFocus=false;

}


void customwidget2::wheelEvent(QWheelEvent *event)
{
    if(!bFocus)
        return;
    QRect tmp = this->geometry();
    QPoint centerPoint = tmp.center(); // 储存中心点坐标
    static int adjustSize = 20;
    if (event->delta() > 0) // 放大
    {
        tmp.setWidth(tmp.width() + adjustSize);
        tmp.setHeight(tmp.height() + adjustSize);
    }
    else // 缩小
    {
        tmp.setWidth(tmp.width() - adjustSize);
        tmp.setHeight(tmp.height() - adjustSize);
    }
    if (tmp.width() > 50) // 限制最小尺寸
    {
        tmp.moveCenter(centerPoint); // 从中心缩放而非左上角处
        this->setGeometry(tmp);
    }else
    {
        tmp.setWidth(50);
        tmp.setHeight(50);
        this->setGeometry(tmp);
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值