有什么方法可以使像素不可见的油漆更光滑吗?
Is there is any way to make the paint more smooth that the pixel is not visible?
我目前正在尝试学习使用 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)
您必须将渲染提示 QPainter::Antialiasing
设置为 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_())
之前:
之后:
我目前正在尝试学习使用 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)
您必须将渲染提示 QPainter::Antialiasing
设置为 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_())
之前:
之后: