I am drawing a line, but how keep the line ‘jag’ free and clean?
This is the code which draws the image above. Here we may notice the image is full of jagged edges.
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
from PyQt4 import QtGui, QtCore
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
self.drawingPanel = DrawingPanel(self)
verticalLayout = QtGui.QVBoxLayout( self )
verticalLayout.addWidget( self.drawingPanel )
self.setLayout( verticalLayout )
self.resize( 400, 300 )
self.setWindowTitle('Review')
self.show()
class DrawingPanel(QtGui.QGraphicsView):
def __init__( self, parent ):
super( DrawingPanel, self ).__init__( parent )
scene = QtGui.QGraphicsScene()
self.setScene( scene )
pencil = QtGui.QPen( QtCore.Qt.black, 2, QtCore.Qt.SolidLine )
self.scene().addLine( QtCore.QLineF(0, 0, 300, 600), pencil )
def main():
app = QtGui.QApplication( sys.argv )
ex = Example()
sys.exit( app.exec_() )
if __name__ == '__main__':
main()
Here there is a zoom on the image:
Some other questions about it:
You must enable antialiasing in QGraphicsView:
class DrawingPanel(QtGui.QGraphicsView):
def __init__(self, parent):
QtGui.QGraphicsView.__init__(self, parent)
self.setRenderHint(QtGui.QPainter.Antialiasing)
[...]