若该文为原创文章,未经允许不得转载
原博主博客地址:https://blog.csdn.net/qq21497936
原博主博客导航:https://blog.csdn.net/qq21497936/article/details/102478062
本文章博客地址:https://blog.csdn.net/qq21497936/article/details/79163130
各位读者,知识无穷而人力有穷,要么改需求,要么找专业人士,要么自己研究
红胖子(红模仿)的博文大全:开发技术集合(包含Qt实用技术、树莓派、三维、OpenCV、OpenGL、ffmpeg、OSG、单片机、软硬结合等等)持续更新中…(点击传送门)
Qt开发专栏:实用技巧(点击传送门)
需求
对三个组合矩形进行比例变化,Demo实现效果如下:
原理解说图
关键代码
void MainWindow::paintEvent(QPaintEvent *event) { QPainter painter(this); painter.save(); painter.setPen(Qt::black); int rectx = _rectx + _dx; int recty = _recty + _dy; int rectWidth = _rectWidth/(float)_width*(_width+_dwidth); int rectHeight = _rectHeight/(float)_height*(_height+_dheight); painter.drawRect(rectx, recty, rectWidth, rectHeight); painter.restore(); painter.save(); painter.setPen(Qt::red); int rect2x = _rectx + _dx + (_rect2x-_rectx)/(float)_rectWidth*rectWidth; int rect2y = _recty + _dy + (_rect2y-_recty)/(float)_rectHeight*rectHeight; int rect2Width = _rect2Width/(float)_rectWidth*rectWidth; int rect2Height = _rect2Height/(float)_rectHeight*rectHeight; painter.drawRect(rect2x, rect2y, rect2Width, rect2Height); painter.restore(); painter.save(); painter.setPen(Qt::blue); int rect3x = _rectx + _dx + (_rect3x-_rectx)/(float)_rectWidth*rectWidth; int rect3y = _recty + _dy + (_rect3y-_recty)/(float)_rectHeight*rectHeight; int rect3Width = _rect3Width/(float)_rectWidth*rectWidth; int rect3Height = _rect3Height/(float)_rectHeight*rectHeight; painter.drawRect(rect3x, rect3y, rect3Width, rect3Height); painter.restore(); }
原博主博客地址:https://blog.csdn.net/qq21497936
原博主博客导航:https://blog.csdn.net/qq21497936/article/details/102478062
本文章博客地址:https://blog.csdn.net/qq21497936/article/details/79163130