我正在使用 Qt 的 Python 系結來開發應用程式。我通過設定自定義鍵值制作了非線性影片,如Qt 檔案中所述:
也可以設定位于開始值和結束值之間的值。然后插值將通過這些點。
QPushButton button("Animated Button");
button.show();
QPropertyAnimation animation(&button, "geometry");
animation.setDuration(10000);
animation.setKeyValueAt(0, QRect(0, 0, 100, 30));
animation.setKeyValueAt(0.8, QRect(250, 250, 100, 30));
animation.setKeyValueAt(1, QRect(0, 0, 100, 30));
animation.start();
目標
我更新我的影片值并在幾個部分中使用它,有時我想讓它成為線性。
問題
我找不到洗掉影片的設定鍵值以使其線性的方法。我嘗試將“startValue”“endValue”設定為我的影片,但它們只是替換了默認影片鍵值(0.0 和 1.0),而我之前設定的自定義鍵值將保留在那里。波紋管是一個示例代碼:
import sys
from PyQt5.QtCore import QRect, QPoint, QPropertyAnimation, QParallelAnimationGroup
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QFrame
class Form(QMainWindow):
def __init__(self):
super().__init__()
self.init_ui()
def init_ui(self):
self.resize(600, 400)
self.setWindowTitle("Form 1")
self.frame = QFrame(self)
self.frame.resize(200, 150)
self.frame.move(20, 20)
self.setStyleSheet("""QFrame {
background-color: orange;
}""")
self.button = QPushButton("Start Animation", self)
self.button.resize(self.button.sizeHint())
self.button.move(20, 300)
self.define_animation()
self.button.clicked.connect(self.frame_anim.start)
self.show()
def define_animation(self):
self.frame_anim = QPropertyAnimation(self.frame, b"geometry")
self.frame_anim.setDuration(1000)
self.frame_anim.setStartValue(self.frame.geometry())
self.frame_anim.setKeyValueAt(0.75, QRect(QPoint(20, 100), self.frame.size()))
self.frame_anim.setKeyValueAt(1, QRect(QPoint(380, 220), self.frame.size()))
self.frame_anim.finished.connect(lambda: print("??Animation key values", self.frame_anim.keyValues()))
self.frame_anim.finished.connect(self.define_new_animation)
def define_new_animation(self):
self.frame_anim.setStartValue(QRect(QPoint(380, 220), self.frame.size()))
self.frame_anim.setEndValue(QRect(QPoint(20, 220), self.frame.size()))
app = QApplication(sys.argv)
form = Form()
sys.exit(app.exec_())
第一個影片是非線性的,我設定了一個自定義鍵值,但下一個將保留該鍵值。我正在尋找一種解決方案,可以在設定自定義鍵值后從影片或任何邏輯方式中洗掉設定的鍵值以使其線性化。
uj5u.com熱心網友回復:
雙方setStartValue并setEndValue不會“清除”目前animatio,但只設定新的開始時間與beginnig狀態,同時使所有其他嵌套物件。
如果要重置它,請使用setStartValue()空映射。
為了更新當前影片,您需要清除現有的映射。這是一個可能的解決方案:
class Form(QMainWindow):
# ...
def define_new_animation(self):
self.frame_anim.setKeyValues({})
self.frame_anim.setStartValue(
QRect(QPoint(380, 220), self.frame.size()))
self.frame_anim.setEndValue(
QRect(QPoint(20, 220), self.frame.size()))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/378517.html
上一篇:Qt二進制檔案在哪里?
