平滑倾斜线
Smoothing an inclined line
我找不到合适的方法来绘制平滑的斜线,而不会在 Qt 中使用 QPainterPath
对象过度像素化。
请注意,我知道在 paintEvent
函数中绘制路径没有意义,为了简单起见,我把它放在那里。我正在尝试直接在中央小部件中画线。
以下是我的代码片段:
void MyObject::paintEvent(QPaintEvent *)
{
QPainterPath aPath;
aPath.moveTo(40.0, 60.0); //random values to try
aPath.lineTo(254, 354.0);
QPainter painter(this);
painter.setPen(QPen(QColor(20, 20, 200), 10, Qt::SolidLine));
painter.drawPath(aPath);
}
这是我得到的结果:
太糟糕了!我能画的美线只有横线、竖线或45°斜线...
如果您正在寻找绘图质量,Qt 文档提供了一个 illustrative example (5.X version) of the use of its render quality flags. Generally, you can use the flags specified here (5.X version), and set them using the QPainter::setRenderHint()
(5.X version) 函数。看看您是否能够使用这些方法达到所需的质量。对于您的代码,您会寻找类似
的内容
QPainter painter(this);
painter.setRenderHint(QPainter::SmoothPixmapTransform, true);
painter.setRenderHint(QPainter::HighQualityAntialiasing, true);
painter.setPen(QPen(QColor(20, 20, 200), 10, Qt::SolidLine));
painter.drawPath(aPath);
我找不到合适的方法来绘制平滑的斜线,而不会在 Qt 中使用 QPainterPath
对象过度像素化。
请注意,我知道在 paintEvent
函数中绘制路径没有意义,为了简单起见,我把它放在那里。我正在尝试直接在中央小部件中画线。
以下是我的代码片段:
void MyObject::paintEvent(QPaintEvent *)
{
QPainterPath aPath;
aPath.moveTo(40.0, 60.0); //random values to try
aPath.lineTo(254, 354.0);
QPainter painter(this);
painter.setPen(QPen(QColor(20, 20, 200), 10, Qt::SolidLine));
painter.drawPath(aPath);
}
这是我得到的结果:
太糟糕了!我能画的美线只有横线、竖线或45°斜线...
如果您正在寻找绘图质量,Qt 文档提供了一个 illustrative example (5.X version) of the use of its render quality flags. Generally, you can use the flags specified here (5.X version), and set them using the QPainter::setRenderHint()
(5.X version) 函数。看看您是否能够使用这些方法达到所需的质量。对于您的代码,您会寻找类似
QPainter painter(this);
painter.setRenderHint(QPainter::SmoothPixmapTransform, true);
painter.setRenderHint(QPainter::HighQualityAntialiasing, true);
painter.setPen(QPen(QColor(20, 20, 200), 10, Qt::SolidLine));
painter.drawPath(aPath);