我有一个基本上是圆形的小部件。我想逐步绘制它,所以我需要逐步绘制(imo)。
通过以下代码,我实现了我想要的。但是,有一个问题。我正在将新事件传递给paintEvent
函数,因为如果不这样做,该图像将不会更新,直到一切都完成了,所以我什么都没有实现。< / p>
import sys
import time
from PyQt5.QtCore import Qt, QRect
from PyQt5.QtWidgets import QApplication, QWidget, QCheckBox, QDesktopWidget
from PyQt5.QtGui import QPen, QPainter, QPaintEvent, QConicalGradient, QColor, QBrush
class Circle(QWidget):
def __init__(self, size, color):
super().__init__()
self.loadingAngle = 0
self.width = 0
self.color = color
self.pixmap_opacity = 1
self.resize(size, size);
self.center()
self.initUI()
def initUI(self):
self.width = 15
self.loadingAngle = 0
self.show()
def center(self):
qr = self.frameGeometry()
cp = QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())
def paintEvent(self, qevent):
self.setWindowFlags(Qt.FramelessWindowHint)
self.setAttribute(Qt.WA_TranslucentBackground)
self.setStyleSheet("background:transparent;")
drawingRect = QRect()
drawingRect.setX(qevent.rect().x() + self.width)
drawingRect.setY(qevent.rect().y() + self.width)
drawingRect.setWidth(qevent.rect().width() - self.width * 2)
drawingRect.setHeight(qevent.rect().height() - self.width * 2)
painter = QPainter(self)
painter.setRenderHint(QPainter.Antialiasing)
gradient = QConicalGradient()
gradient.setCenter(drawingRect.center())
gradient.setAngle(90)
gradient.setColorAt(1, QColor(0,0,0))
gradient.setColorAt(0, QColor(self.color[0], self.color[1],self.color[2]))
arcLengthApproximation = self.width + self.width / 3
pen = QPen(QBrush(gradient), self.width)
pen.setCapStyle(Qt.RoundCap)
painter.setPen(pen)
painter.drawArc(drawingRect, 90 * 16 - arcLengthApproximation, -self.loadingAngle * 16)
#time.sleep(0.25)
if self.loadingAngle < 360:
self.loadingAngle += 1
#self.paintEvent(QDrawEvent())
self.paintEvent(QPaintEvent())
self.paintEvent(QPaintEvent())
此行会产生一些错误,但是即使有错误,我也会按照我的意愿做。
如果我将qevent
从函数本身传递到此新调用,则图像不会像我之前说的那样得到更新。
如果我创建了这个新的QPaintEvent
,它确实可以工作。但是,错误是:
回溯(最近通话最近一次):
文件“ /home/btc/Escritorio/SinestesiaRCB/Clases/Widget.py”,第68行,在 paintEvent self.paintEvent(QPaintEvent())
TypeError:参数与任何重载调用都不匹配: QPaintEvent(QRegion):参数不足
QPaintEvent(QRect):参数不足
QPaintEvent(QPaintEvent):参数不足
QBackingStore :: endPaint()在后备存储绘制设备上用活动绘制程序调用
这些错误可能来自其他行,例如:
qevent.rect().x()
由于新事件是空事件。
所以基本上我的问题是,我应该如何做才能正确完成它,这意味着无误地实现我想要的东西?
PS。我的意思是逐步地。完成此操作后,创建了几个小部件,每个小部件之后是一秒钟。
答案 0 :(得分:1)
永远不要直接调用paintEvent,必须使用update()
间接调用它。另一方面,如果您想经常被调用,在这种情况下,您应该使用QTimer或更好的QTimeLine。
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Circle(QtWidgets.QWidget):
def __init__(self, size, color):
super().__init__()
self._loading_angle = 0
self.width = 0
self.color = color
self.pixmap_opacity = 1
self.resize(size, size)
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
self.setAttribute(QtCore.Qt.WA_TranslucentBackground)
self.setStyleSheet("background:transparent;")
self.center()
self.initUI()
timeline = QtCore.QTimeLine(4000, self)
timeline.setFrameRange(0, 360)
timeline.frameChanged.connect(self.setLoadingAngle)
timeline.start()
def initUI(self):
self.width = 15
self.setLoadingAngle(0)
self.show()
def loadingAngle(self):
return self._loading_angle
def setLoadingAngle(self, angle):
self._loading_angle = angle
self.update()
loadingAngle = QtCore.pyqtProperty(int, fget=loadingAngle, fset=setLoadingAngle)
def center(self):
qr = self.frameGeometry()
cp = QtWidgets.QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())
def paintEvent(self, event):
painter = QtGui.QPainter(self)
painter.setRenderHint(QtGui.QPainter.Antialiasing)
drawingRect = QtCore.QRect(QtCore.QPoint(), self.rect().size() - 2*self.width*QtCore.QSize(1, 1))
drawingRect.moveCenter(self.rect().center())
gradient = QtGui.QConicalGradient()
gradient.setCenter(drawingRect.center())
gradient.setAngle(90)
gradient.setColorAt(1, QtGui.QColor(0,0,0))
gradient.setColorAt(0, self.color)
arcLengthApproximation = self.width + self.width / 3
pen = QtGui.QPen(QtGui.QBrush(gradient), self.width)
pen.setCapStyle(QtCore.Qt.RoundCap)
painter.setPen(pen)
painter.drawArc(drawingRect, 90 * 16 - arcLengthApproximation, -self._loading_angle * 16)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = Circle(400, QtGui.QColor("blue"))
w.show()
sys.exit(app.exec_())