有什么方法可以使涂料更平滑,使像素不可见?

时间:2018-10-01 04:40:25

标签: python pyqt pyqt4 qpainter

我目前正在尝试使用PyQt4库在python中制作UI。我试图制作一个服务器-客户端聊天应用程序。当我尝试在聊天中添加气泡时,我注意到像素太明显了。

有什么方法可以使画图更平滑,使像素不可见?

下面是我的代码

def paintEvent(self,event):
    painter = QPainter(self)

    painter.setBrush(QBrush(Qt.white, Qt.SolidPattern))
    painter.setPen(QPen(Qt.white, 5, Qt.SolidLine))

    start_x =50
    start_y =50
    luar = QPolygon([
        QPoint(start_x,start_y),
        QPoint(start_x+310,start_y-10),
        QPoint(start_x+274,start_y+60),
        QPoint(start_x-20,start_y+58),

        ])
    painter.drawPolygon(luar)

    painter.setPen(QPen(Qt.black, 5, Qt.SolidLine))
    painter.setBrush(QBrush(Qt.black, Qt.SolidPattern))
    start_x +=5
    start_y +=5

    points = QPolygon([
        QPoint(start_x,start_y),
        QPoint(start_x+290,start_y-10),
        QPoint(start_x+270,start_y+50),
        QPoint(start_x-20,start_y+48),

        ])
    painter.drawPolygon(points)

1 个答案:

答案 0 :(得分:0)

您必须使用setRenderHint()将渲染提示设置为QPainter::Antialiasing到True:

from PyQt4 import QtCore, QtGui

class Widget(QtGui.QWidget):
    def paintEvent(self, event):
        painter = QtGui.QPainter(self)
        painter.setRenderHint(QtGui.QPainter.Antialiasing, True)

        painter.setBrush(QtGui.QBrush(QtCore.Qt.white, QtCore.Qt.SolidPattern))
        painter.setPen(QtGui.QPen(QtCore.Qt.white, 5, QtCore.Qt.SolidLine))

        start_pos = QtCore.QPoint(50, 50)

        luar = QtGui.QPolygon([
            QtCore.QPoint(0, 0),
            QtCore.QPoint(310, -10),
            QtCore.QPoint(274, 60),
            QtCore.QPoint(-20, 58)
            ]).translated(start_pos)

        painter.drawPolygon(luar)

        painter.setPen(QtGui.QPen(QtCore.Qt.black, 5, QtCore.Qt.SolidLine))
        painter.setBrush(QtGui.QBrush(QtCore.Qt.black, QtCore.Qt.SolidPattern))

        start_pos += QtCore.QPoint(5, 5)

        points = QtGui.QPolygon([
            QtCore.QPoint(0, 0),
            QtCore.QPoint(290, -10),
            QtCore.QPoint(270, 50),
            QtCore.QPoint(-20, 48)
            ]).translated(start_pos)

        painter.drawPolygon(points)

if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    w = Widget()
    w.resize(400, 150)
    w.show()
    sys.exit(app.exec_())

之前:

enter image description here

之后:

enter image description here