如何平滑 pyqt4 上曲线和对角线上的锯齿状边缘?

How to smoothing jagged edges on curved lines and diagonals on pyqt4?

我正在画一条线,但如何保持线条“锯齿”的自由和干净?

这是绘制上图的代码。在这里我们可能会注意到图像充满了锯齿状的边缘。

#!/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()

这里有放大图片:

关于它的一些其他问题:

  1. Smoothing jagged edges without anti-aliasing - Unity3D

您必须在 QGraphicsView 中启用抗锯齿功能:

class DrawingPanel(QtGui.QGraphicsView):

    def __init__(self, parent):
        QtGui.QGraphicsView.__init__(self, parent)
        self.setRenderHint(QtGui.QPainter.Antialiasing)
        [...]