我有一个基本上是一个圆的小部件。我想逐步画,所以我需要画在步骤(imo)。在
通过下面的代码,我已经实现了我想要的。但是,有一个问题。我将一个新事件传递给paintEvent
函数,因为如果我不这样做,图像在所有事情都完成之前不会更新,所以我什么都没有实现。在
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())
这一行产生了几个错误,但即使有了它们,我也会做我想做的。在
如果我将qevent
从函数本身传递给这个新调用,那么图像不会像我之前说的那样得到更新。
如果我创建这个新的QPaintEvent
,它确实可以工作。但是,错误是:
Traceback (most recent call last):
File "/home/btc/Escritorio/SinestesiaRCB/Clases/Widget.py", line 68, in paintEvent self.paintEvent(QPaintEvent())
TypeError: arguments did not match any overloaded call: QPaintEvent(QRegion): not enough arguments
QPaintEvent(QRect): not enough arguments
QPaintEvent(QPaintEvent): not enough arguments
QBackingStore::endPaint() called with active painter on backingstore paint device
这些错误可能来自其他行,如:
qevent.rect().x()
因为新事件是空的。在
所以基本上我的问题是,我应该怎样做才能使它正确,也就是说,实现我想要的而不出错?在
我所说的渐进式。这已经完成了创建几个小部件,每一个都在前一个小部件之后一秒钟。在
永远不要直接调用paintEvent,必须使用
update()
间接调用它。另一方面,如果您希望每隔一段时间被调用,那么在本例中应该使用QTimer或更好的QTimeLine。在相关问题 更多 >
编程相关推荐