简体   繁体   中英

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. 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).

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.

Using only AntiAliasing hint produces a smooth line for 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')

画线演示

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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