2016-09-30 94 views
1

我需要在SwipeView動畫結束後使用UI中的參數調用C++類方法。QML動畫完成後調用C++函數

main.ui

ApplicationWindow { 
visible: true 
width: 640 
height: 480 
title: qsTr("Hello World") 

SwipeView { 
    id: swipeView 
    anchors.fill: parent 

    Page1 { 
     id: page1 
    } 

    Page2{ 
     id: page2 
    } 
} 

XOR { 
    id: xor 
    onXorEnded: { 
     //swipeView.setCurrentIndex(0) 
    } 
    onQChanged: { 
     page2.bar.value = xor.getq() 
    } 
} 

}

Page1Form.ui.qml

Page1Form { 



kpButton.onClicked: { 
    kpDialog.visible = true 
} 

xorButton.onClicked: { 
    swipeView.setCurrentIndex(1) 
    xor.crypt(file_path.text, key_path.text, out_path.text) 
} 

fpButton.onClicked:{ 
    fpDialog.visible = true 
} 


FileDialog { 
    id: fpDialog 
    onAccepted: { 
     file_path.text = fpDialog.fileUrl 
    } 
} 

FileDialog { 
    id: kpDialog 
    onAccepted: { 
     key_path.text = kpDialog.fileUrl 
    } 
} 
} 

好像在xorButton.onClicked異或開始滑動視圖的動畫結束之前。它是如何工作現在:Imgur

+0

SwipeView無法訪問其動畫,也無法設置自己的動畫,因此沒有簡單的方法來完成它。這是QML控件中內置的典型特徵,也是我不使用它們的原因。你要麼滿足於它所提供的東西,要麼想要功能性和靈活性,而要自己控制。 – dtech

+0

你的問題是你的'xor.crypt'方法是同步的(它只在所有數據計算完成後才返回),如果你想要你想要的行爲,你應該使它成爲異步的。 – GrecKo

回答

4

作爲一種解決方法您可以將動作綁定到指數變化:

xorButton.onClicked: { 
    swipeView.setCurrentIndex(1) 
} 


SwipeView { 
    id: swipeView 
    onCurrentItemChanged: { 
     if(currentIndex == 1) 
      xor.crypt(file_path.text, key_path.text, out_path.text) 
    } 
} 

但無論如何,這不是火災,在動畫的結尾。

作爲另一種解決方法,您可以使用StackView。它有更多的屬性來控制動畫。這種控制的另一個優點是用戶無法在不期望的情況下刷卡。在你的情況下,用戶只需輕掃即可。還有一個好處是,當你不需要它時,頁面不會佔用內存。

import QtQuick 2.7 
import QtQuick.Window 2.2 
import QtQuick.Controls 2.0 


Window { 
    visible: true 
    width: 800 
    height: 800 
    StackView { 
     id: view 
     anchors.fill: parent 
     initialItem: page1 
     onBusyChanged: { 
      if(!busy && currentItem.objectName == "page2") 
       currentItem.run(); 
     } 
    } 
    Component { 
     id: page1 
     Rectangle { 
      color: "green" 
      objectName: "page1" 
      Button { 
       anchors.centerIn: parent 
       text: "swipe me" 
       onClicked: 
        view.push(page2) 
      } 
     } 
    } 

    Component { 
     id: page2 
     Rectangle { 
      color: "yellow" 
      objectName: "page2" 
      function run() { sign.visible = true; } 

      Rectangle { 
       id: sign 
       anchors.centerIn: parent 
       width: 100 
       height: 100 
       radius: 50 
       color: "red" 
       visible: false 
      } 
     } 
    } 

} 
+0

非常感謝:) –