QT实现的可移动放大缩小的大小嵌套窗体
发布日期:2022-02-24 11:35:57 浏览次数:6 分类:技术文章

本文共 2672 字,大约阅读时间需要 8 分钟。

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

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

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);

转载地址:https://blog.csdn.net/hulinhulin/article/details/121407323 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:网站信息采集系统(四)--京东商品信息的采集
下一篇:QT实现的具有放大功能的小窗口

发表评论

最新留言

关注你微信了!
[***.104.42.241]2024年03月13日 14时26分11秒