zl程序教程

您现在的位置是:首页 >  工具

当前栏目

Qt之描绘轮廓

Qt 轮廓
2023-09-11 14:19:13 时间

前面提到过 QPainterPath(绘图路径),除了创建和重用图形形状以外,还可以进行一些高级操作,例如:填充、描绘轮廓、裁剪。


定义原始绘图路径 QPainterPath path,调用 QPainterPathStroker::createStroke(path),以 path 为样本创建一个新的绘图路径 QPainterPath outlinePath,outlinePath 表示 path 的可填充的轮廓。 填充 outlinePath,绘制出原始绘图路径 path 的轮廓。

除此之外,还可以使用以下函数来控制轮廓的各种样式:


注意: 由 createStroke() 生成的新绘图路径(outlinePath)应仅用于指定的绘图路径(path)的轮廓,否则,可能会引发意外行为。


stroker.setCapStyle(Qt::RoundCap); // 端点风格 stroker.setJoinStyle(Qt::RoundJoin); // 连接样式 stroker.setDashPattern(Qt::DashLine); // 虚线图案 stroker.setWidth(10); // 宽度 // 生成一个新路径(可填充区域),表示原始路径 path 的轮廓 QPainterPath outlinePath = stroker.createStroke(path); // 绘制轮廓时所用的画笔(轮廓外边框灰色部分) QPen pen = painter- pen(); pen.setColor(QColor(0, 160, 230)); pen.setWidth(10); // 用指定的画笔 pen 绘制 outlinePath // painter- strokePath(outlinePath, pen); painter- setPen(pen); painter- drawPath(outlinePath); // 用指定的画刷 brush 填充路径 outlinePath painter- fillPath(outlinePath, QBrush(Qt::yellow)); }

轮廓有了,来看看原始路径的绘制:


回到 paintEvent(),如果将 drawPath() 和 drawOutline() 的位置颠倒过来,就会产生下面的效果:

这里写图片描述

很显然,原始路径会将轮廓遮挡住一部分,这是为什么呢?

关于 QPainterPathStroker::setWidth(),助手中有这么一句话:


The generated outlines will extend approximately 50% of width to each side of the given input path’s original outline.


分别用 VTK 体绘制和面绘制来实现医学图像三维重建 序言,VTK介绍: VTK 全称为 The Visualization Toolkit (可视化工具),是一个开源、跨平台、自由获取、支持并行计算的图形应用函数;拥有3D 渲染的最新工具、提供3D交互模式以及2D绘图等。