简体   繁体   English

PySide:是否可以绘制平滑线 QPainter?

[英]PySide: is it possible to draw a smoothed line QPainter?

When I draw a line using QPainter, it moves jaggedly from pixel to pixel, even if I apply antialiasing.当我使用 QPainter 绘制一条线时,即使我应用了抗锯齿,它也会在像素之间呈锯齿状移动。 Is it possible to draw a line like you'd draw in photoshop, where it moves from pixel to pixel evenly (ie. resulting in grey pixels if the line position is not an exact pixel value).是否可以像在 Photoshop 中那样绘制一条线,它在像素之间均匀移动(即,如果 position 线不是精确的像素值,则会导致灰色像素)。

Here's an example showing a jagged line:这是一个显示锯齿线的示例:

from PySide import QtGui

_pix = QtGui.QPixmap(640, 640)
_pix.fill('White')
_pen = QtGui.QPen("Black")

_pnt = QtGui.QPainter()
_pnt.begin(_pix)
_pnt.setPen(_pen)
_pnt.setRenderHint(_pnt.HighQualityAntialiasing, 1)
_pnt.drawLine(640.0/2-50, 10, 640.0/2+50, 640.0-10)
_pnt.end()

_pix.save('C:/temp/test.jpg', 'JPG')

I guess I could draw the image at 4x size and then reduce it, but I was wondering if there's a way to avoid drawing all those extra pixel which would prob be quite expensive.我想我可以将图像绘制为 4 倍大小,然后将其缩小,但我想知道是否有办法避免绘制所有可能非常昂贵的额外像素。

Using only AntiAliasing hint produces a smooth line for Qt 4:仅使用AntiAliasing提示会为 Qt 4 生成一条平滑线:

from PySide import QtGui

qapp = QtGui.QApplication([])

_pix = QtGui.QPixmap(640, 640)
_pix.fill('White')
_pen = QtGui.QPen("Black")

_pnt = QtGui.QPainter()
_pnt.begin(_pix)
_pnt.setPen(_pen)
_pnt.setRenderHint(_pnt.Antialiasing, True)
_pnt.drawLine(640.0/2-50, 10, 640.0/2+50, 640.0-10)
_pnt.end()
_pix.save('test.jpg', 'JPG')

画线演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM