Search code examples
pythonpyqtpyqt4qpainter

Is there is any way to make the paint more smooth that the pixel is not visible?


I'm currently try to learn making UI in python using PyQt4 library. I tried to make a server-client chatting app. when i try to make the bubble for the chat i noticed that the pixel is too obvious.

Is there is any way to make the paint more smooth that the pixel is not visible?

Below is my code if necessary

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)

Solution

  • You must set the render hint to QPainter::Antialiasing to True with setRenderHint():

    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_())
    

    Before:

    enter image description here

    After:

    enter image description here