QT 范例阅读:Vector Deformation

效果图:

主要代码:

复制代码
实现放大镜效果
    QPainter painter;
    //两种方式
    if (1) {
        m_lens_image = QImage(bounds.size(), QImage::Format_ARGB32_Premultiplied);
        m_lens_image.fill(0);
        painter.begin(&m_lens_image);
    } else {
        m_lens_pixmap = QPixmap(bounds.size());
        m_lens_pixmap.fill(Qt::transparent);
        painter.begin(&m_lens_pixmap); //将pixmap 当做绘图设备
    }
    
    //使用渐变的方式画圆 
    QRadialGradient gr(rad, rad, rad, 3 * rad / 5, 3 * rad / 5);
    gr.setColorAt(0.0, QColor(255, 255, 255, 191));
    gr.setColorAt(0.2, QColor(255, 255, 240, 191));
    gr.setColorAt(0.9, QColor(150, 150, 200, 63));
    gr.setColorAt(0.95, QColor(0, 0, 0, 127));
    gr.setColorAt(1, QColor(0, 0, 0, 0));
    painter.setRenderHint(QPainter::HighQualityAntialiasing);
    painter.setBrush(gr);
    painter.setPen(Qt::NoPen);
    painter.drawEllipse(0, 0, bounds.width(), bounds.height());

    //初始化文字路径

    QFontMetrics fm(f);

    m_paths.clear();  //路径 QVector<QPainterPath>
    m_pathBounds = QRect(); //整串文字大小边框

    QPointF advance(0, 0); //文字起始点

    bool do_quick = true;
    for (int i=0; i<text.size(); ++i) {
        if (text.at(i).unicode() >= 0x4ff && text.at(i).unicode() <= 0x1e00) {
            do_quick = false;
            break;
        }
    }

    if (do_quick) {
        for (int i=0; i<text.size(); ++i) {
            QPainterPath path;
            path.addText(advance, f, text.mid(i, 1)); //添加文字路径
            m_pathBounds |= path.boundingRect();
            m_paths << path;
            advance += QPointF(fm.horizontalAdvance(text.mid(i, 1)), 0);
        }
    } else {
        QPainterPath path;
        path.addText(advance, f, text);
        m_pathBounds |= path.boundingRect();
        m_paths << path;
    }

//放大镜内的文字变形
QPainterPath MainWindow::lensDeform(const QPainterPath &source, const QPointF &offset)
{
    QPainterPath path;
    path.addPath(source);  //传进来的就是m_paths[i]

    qreal flip = 100 / qreal(100);

    for (int i=0; i<path.elementCount(); ++i) {  
        const QPainterPath::Element &e = path.elementAt(i);  

        qreal x = e.x + offset.x();
        qreal y = e.y + offset.y();

        qreal dx = x - m_pos.x();
        qreal dy = y - m_pos.y();
        qreal len = m_radius - qSqrt(dx * dx + dy * dy);

        //判断在不在圆内,在则修改路径各个点的位置
        if (len > 0) {
            path.setElementPositionAt(i,
                                      x + flip * dx * len / m_radius,
                                      y + flip * dy * len / m_radius);
        } else {
            path.setElementPositionAt(i, x, y);
        }

    }

    return path;
}

绘图更新:

可以只更新 特定区域 ;如 update(rectBefore | rectAfter); //参数是两个矩形

paintEvent(QPaintEvent *e) 中 可以使用 e->rect(); 获取到要刷新的区域

相关推荐
小鱼仙官3 小时前
Windows Qt调用Vs库实现UDP双口接收数据
开发语言·qt
rit84324994 小时前
基于Qt的串口上位机控制蓝牙小车程序
开发语言·qt
luoyayun3614 小时前
Qt/QML 音频频谱图与频谱瀑布图实现:从 PCM 到频域可视化
qt·音视频·频谱图·频谱瀑布图
爱吃生蚝的于勒5 小时前
QT开发第三章——常用控件
linux·服务器·开发语言·前端·javascript·c++·qt
Shadow(⊙o⊙)5 小时前
QT常用控件1.0,enabled() geometry() QIcon的.qrc文件导入
开发语言·c++·qt
小短腿的代码世界5 小时前
高性能订单路由与智能拆单算法:Qt在量化交易系统中的核心架构——毫秒级延迟下如何隐藏你的交易意图?
开发语言·qt·架构
油炸自行车5 小时前
【bug】Qt 6 Q_NAMESPACE 跨 DLL 链接错误:LNK2019 无法解析 staticMetaObject
数据库·c++·qt·bug·link2019·q_namespace_exp·namespaceexport
Dovis(誓平步青云)6 小时前
《QT学习第五篇:QSS美化界面与API绘图》
开发语言·数据库·qt·学习·时序数据库·开源智能体
ChindongX6 小时前
笔记:解决窗口透明度不生效问题 —— QGraphicsOpacityEffect 的使用
笔记·qt
Shadow(⊙o⊙)6 小时前
QT常用控件3.0,font字体设置,toolTip提示,focusPolicy焦点定位原则,中型控件StyleSheet样式表。
服务器·开发语言·前端·c++·qt