2017-05-28 167 views
0

我正在畫一條線,但是如何保持線條'jag'免費且乾淨?如何平滑pyqt4上的曲線和對角線上的鋸齒狀邊緣?

enter image description here

這是上述繪製圖像的代碼。在這裏,我們可能會注意到圖像充滿了鋸齒狀的邊緣。

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

這裏有圖片的縮放:

enter image description here

關於它的一些其他問題:

  1. Smoothing jagged edges without anti-aliasing - Unity3D
  2. How to enable anti-aliasing in A-Frame?

回答

1

您必須在QGraphicsView中啓用抗鋸齒功能:

class DrawingPanel(QtGui.QGraphicsView): 

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

謝謝。也可以這樣做:'self.setRenderHint(QtGui.QPainter.Alliiasing); self.setRenderHint(QtGui.QPainter.TextAntialiasing); self.setRenderHint(QtGui.QPainter.SmoothPixmapTransform); self.setRenderHint(QtGui.QPainter.HighQualityAlliiasing);'? – user