2011-09-05 70 views
1

對不起,我的英文破碎。PyQt動畫框架和狀態機中的可見屬性

我正在使用帶有動畫的狀態機。我想實現淡出/有效。

STATE1: opactiy = 0 可見=假

STATE2: opactiy = 1 可見=真

STATE1 - > STATE2: 從底部到頂部,將外集件組「可見「屬性爲」真「,然後在5秒內將」opactiy「設置爲012到0.15,並將其設置爲 。

state2 - > state1:​​ 將小部件從上往下移動,其「opactiy」在5秒內從1.0變爲0.0,然後將小部件的「visible」屬性設置爲False。

但問題是,當state2到state1時,它總是首先將「visible」屬性設置爲False,所以我看到的是小部件消失而沒有淡出效果,即使我先使用QSequentialAnimationGroup並設置opactiy_animation。

如何獲得淡出效果?

驗證碼:

self.group_p = QtCore.QParallelAnimationGroup() 
self.group_s = QtCore.QSequentialAnimationGroup() 
self.group_sr = QtCore.QSequentialAnimationGroup() 

goe = QtGui.QGraphicsOpacityEffect() 
self.label_arrow.setGraphicsEffect(goe) 
animation_o = QtCore.QPropertyAnimation(goe, "opacity")    
animation_g = QtCore.QPropertyAnimation(self.label_arrow, "geometry") 
animation_v = QtCore.QPropertyAnimation(self.label_arrow, "visible") 

animation_g.setDuration(5000) 
animation_g.setEasingCurve(QEasingCurve.OutBounce) 
animation_o.setDuration(5000) 

self.group_p.addAnimation(animation_g) 
self.group_p.addAnimation(animation_o)                  

self.group_s.addAnimation(self.group_p) 
self.group_s.addAnimation(animation_v) 

self.group_sr.addAnimation(animation_v) 
self.group_sr.addAnimation(self.group_p) 

self.machine = QtCore.QStateMachine() 
state1 = QState(self.machine) 
state2 = QState(self.machine) 
self.machine.setInitialState(state1) 

state1.assignProperty(self.label_arrow, "geometry", QtCore.QRect(self.label_arrow.x(),\ 
         self.label_arrow.y()+100, self.label_arrow.width(), self.label_arrow.height())) 
state1.assignProperty(self.label_arrow, "visible", False) 
state1.assignProperty(goe, "opacity", 0.5)          

state2.assignProperty(self.label_arrow, "geometry", self.label_arrow.geometry()) 
state2.assignProperty(self.label_arrow, "visible", True) 
state2.assignProperty(goe, "opacity", 1)       

transition = state1.addTransition(self.sig_arrow_animate, state2) 
transition.addAnimation(self.group_sr)        

transition2 = state2.addTransition(self.sig_arrow_animate, state1) 
transition2.addAnimation(self.group_s) # set visible to False first! 
self.machine.start() 

回答

0

你不能動畫visible屬性,因爲bool不是動畫支持的類型。您應該將其從動畫中移除,並且僅對該屬性使用assignProperty

根據文檔,最終值也由動畫結束後的QState分配。這就是爲什麼即使動畫不起作用,結果值是正確的((編輯),並且該段落不是很有幫助,因爲bool的動畫未執行,因此狀態立即更改)。

你需要在2子狀態來劃分state1,先用「幾何體」和「不透明」屬性,第二,通過在所有對當前狀態的動畫是成品所發出的信號propertiesAssigned()觸發,具有「可見」屬性。

state1_1 = QState(state1) 
state1_2 = QState(state1) 
state1.setInitialState(state1_1) 

state1_1.assignProperty(self.label_arrow, "geometry", QtCore.QRect(self.label_arrow.x(),\ 
         self.label_arrow.y()+100, self.label_arrow.width(), self.label_arrow.height())) 
state1_1.assignProperty(goe, "opacity", 0.5) 

state1_2.assignProperty(self.label_arrow, "visible", False) 
state1_1.addTransition(state1_1.propertiesAssigned, state1_2) 
... 
+0

謝謝。我知道你的意思,我刪除animation_v,並使用assignProperty爲「可見」屬性。但它的工作原理是一樣的。當狀態改變時,它首先分配「可見」屬性,而不是在動畫結束之後。我錯過了什麼? – lSaint

+0

@lSaint我誤解了問題所在,我編輯了我的答案以提供解決方案。 – alexisdm

+0

太棒了!這行得通。再次感謝。順便說一下,它需要將QFinalState更改爲QState,因爲'QFinalState'對象沒有'assignProperty'屬性:) – lSaint