QT实现的可移动放大缩小的大小嵌套窗体

一大一小两个窗口,小的在大的里面,可移动、放大缩小,移动时两个一起移动,可以对两个窗口单独放大缩小,效果如下图所示:

QT实现的可移动放大缩小的大小嵌套窗体_第1张图片

本文只是截取的一部分代码,如果需要完整代码可以访问下面的地址,代码是完整的测试项目,真正实现的类是PatchWindow类:

QT实现的可移动放大缩小的大小嵌套窗体-互联网文档类资源-CSDN下载

1 窗口透明及重绘线条

设置窗口透明度:setWindowOpacity(0.0);

重载paintEvent函数,handleSize是拖动手柄的宽:

    painter.setPen(QPen(Qt::red,borderWidth,Qt::SolidLine,Qt::RoundCap,Qt::RoundJoin));

    int hs = handleSize / 2;

    if (this->hasFocus()){

        painter.drawRect(borderWidth,borderWidth,handleSize,handleSize);

        painter.drawRect(this->width() - handleSize - borderWidth,borderWidth,handleSize,handleSize);

        painter.drawRect(this->width() - handleSize - borderWidth,this->height() - handleSize - borderWidth,handleSize,handleSize);

        painter.drawRect(borderWidth,this->height() - handleSize - borderWidth,handleSize,handleSize);

        painter.drawLine(QPointF(handleSize + borderWidth,hs + borderWidth),QPointF(this->width() - handleSize - borderWidth,hs + borderWidth));

        painter.drawLine(QPointF(this->width() - hs - borderWidth,handleSize + borderWidth),QPointF(this->width() - hs - borderWidth,this->height() - handleSize - borderWidth));

        painter.drawLine(QPointF(hs + borderWidth,handleSize + borderWidth),QPointF(hs + borderWidth,this->height() - handleSize - borderWidth));

        painter.drawLine(QPointF(handleSize + borderWidth,this->height() - hs - borderWidth),QPointF(this->width() - handleSize - borderWidth,this->height() - hs - borderWidth));

}

2 窗口移动

重载mouseMoveEvent函数,当按下窗口时:

    CursorRegion cr = getCursorRegion(event->pos());

    if (mousePressed){

        QPoint _curPos = this->mapToParent(event->pos());

        QPoint _offPos = _curPos - previousPos;

        //move window

        QRect _curRect = this->geometry();

        QRect _moveRect(_curRect.topLeft(), _curRect.bottomRight());

}

CursorRegion定义如下:

enum CursorRegion{

    NONE,

    TOPLEFT,

    TOPRIGHT,

    BOTTOMRIGHT,

    BOTTOMLEFT

};

3 窗口放大缩小

这里只放了拖动左上角时的代码,: 

QRect _curRect = this->geometry();

QRect _moveRect(_curRect.topLeft(), _curRect.bottomRight());           _moveRect.setLeft(_moveRect.topLeft().x() + _offPos.x());

_moveRect.setRight(_moveRect.bottomRight().x() - _offPos.x());

_moveRect.setTop(_moveRect.topLeft().y() + _offPos.x());

_moveRect.setBottom(_moveRect.bottomRight().y() - _offPos.x());

this->setGeometry(_moveRect);

4 设置内部小窗口及初始化位置

    this->innerWindow = win;

    //initialize win's geometry

    QPoint _center = this->geometry().center();

    int _width = this->width() / 4;

    int _height = this->height() / 4;

    QPoint _topLeft,_bottomRight;

    _topLeft.setX(_center.x() - _width);

    _topLeft.setY(_center.y() - _height);

    _bottomRight.setX(_center.x() + _width);

    _bottomRight.setY(_center.y() + _height);

    win->setGeometry(QRect(_topLeft,_bottomRight));

5 使用方式

    patch = new PatchWindow(this);

    patch->setGeometry(50,50,200,200);

    innerPatch = new PatchWindow(this);

    patch->setInnerWindow(innerPatch);

你可能感兴趣的:(C++,/,QT,c++,qt)