c++ - Qt画一个带孔的环/圆

标签 c++ qt qpainter

我需要用 QPainter 画一个圆。当我像这样使用 drawEllipse 函数时:

void UserClass::Draw(QPainter &painter) {

    painter.save();
    painter.setBrush( GetColor() );
    QPoint centerPosition = GetCenterPosition();
    painter.drawEllipse( centerPosition, m_CircleOuterRadius, m_CircleOuterRadius);
    painter.setBrush(QColor(0, 0, 0, 0));
    painter.drawEllipse( centerPosition, m_CircleInnerRadius, m_CircleInnerRadius);
    painter.restore();  
}

不幸的是结果不是我想要的。我想让内圈不被填满。这就是为什么我将 alpha 值设为零,但当然它不起作用。我怎么能有一个直到一定半径才用 qt 的圆?

enter image description here

最佳答案

您应该创建一个QPainterPath,然后通过addEllipse() 向其中添加两个圆圈,首先是外部圆圈,然后是内部圆圈。这将有效地为您提供一个形状,即外圆和内圆打洞。

然后用绿色画笔填充画家路径,这将产生一个空心环。之后,如果你想要白色的轮廓,你也可以用白笔在路径上描边。

另请注意,画家路径只能创建一次并存储以供重复使用,而不是每次重绘时都重新创建。

关于c++ - Qt画一个带孔的环/圆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44371863/

相关文章:

c++ - 什么是 undefined reference /未解析的外部符号错误,我该如何解决?

c++ - 线程数之间的事件c++

c++ - Qt C++ 删除 QList<float**> 中的 float** 元素

c++ - QPainterPaths 的高效离屏渲染(需要 OpenGL 和非 OpenGL 解决方案)

python - 使用 QPainter 更改线条的位置

c++ - boost::exception 和 std::exception 之间的关系

c++ - 如何为 C++ 制作一个简单的可嵌入脚本语言?

c++ - Qt connect() 用法

multithreading - 带有线程类的 Qt 信号和槽

c++ - 在 QPainter::end() 之前调用 QImage::save() 是否安全