PyQt5: при изменении размера/движении виджет не успевает до конца отрисоваться, как это исправить? (Eng in description)
Вот видео происходящего (при изменении размера/движении появляются белые полосы/виджет не успевает отрисоваться) Here is a video of what is happening (when resizing / moving, white stripes appear / the widget does not have time to draw)
Video (without download): https://disk.yandex.ru/d/ZoEL0EWCeDgB6w
UPD:
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class Button(QPushButton):
def __init__(self, parent=None):
super().__init__(parent)
self.setText("Кнопка")
self.resize(200, 50)
self.move(100, 100)
self.setStyleSheet("background-color: rgb(30, 30, 30); color: white; border: 2px solid rgb(215, 150, 100); border-radius: 10px")
self.size_anim = QVariantAnimation()
self.size_anim.setStartValue(self.size())
self.size_anim.setEndValue(QSize(150, 30))
self.size_anim.setDuration(100)
self.size_anim.valueChanged.connect(self.resize)
self.move_anim = QVariantAnimation()
self.move_anim.setStartValue(self.pos())
self.move_anim.setDuration(100)
self.move_anim.valueChanged.connect(self.move)
def mousePressEvent(self, e: QMouseEvent):
if e.button() == Qt.MouseButton.LeftButton:
self.size_anim.setDirection(QVariantAnimation.Direction.Forward)
self.size_anim.start()
self.move_anim.setDirection(QVariantAnimation.Direction.Forward)
x = self.parent().mapFromGlobal(self.cursor().pos()).x() - self.width() / 4
y = self.parent().mapFromGlobal(self.cursor().pos()).y() - self.height() / 4
self.move_anim.setStartValue(self.pos())
self.move_anim.setEndValue(QPoint(x, y))
self.move_anim.start()
self.dragPos = e.globalPos()
e.accept()
return super().mousePressEvent(e)
def mouseReleaseEvent(self, e: QMouseEvent):
if e.button() == Qt.MouseButton.LeftButton:
self.size_anim.setDirection(QVariantAnimation.Direction.Backward)
self.size_anim.start()
return super().mouseReleaseEvent(e)
def mouseMoveEvent(self, e: QMouseEvent):
pos = self.pos() + e.globalPos() - self.dragPos
self.move(pos)
self.dragPos = e.globalPos()
e.accept()
def resize(self, size: QSize, *args):
return super().resize(size, *args)
class Window(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle("Тест анимации")
self.resize(1280, 720)
self.setStyleSheet("background-color: rgb(50, 50, 50)")
self.button = Button(self)
if __name__ == "__main__":
app = QApplication([])
window = Window()
window.show()
app.exec()