2016-11-08 47 views
0

我一直試圖運行BusyIndi​​cator(http://doc.qt.io/qt-5/qml-qtquick-controls-busyindicator.html),而我加載一個qml文件(http://doc.qt.io/qt-5/qml-qtquick-loader.html),但BusyIndi​​cator沒有出現。QML BusyIndi​​cator,同時加載一個沉重的qml文件

我想要做的是: 1-用戶發出一個「handlerLoader(name)」,其中「name」是下一個qml頁面的url。 2-在「onHandlerLoader」中,我運行busyIndi​​cator。 3,然後,我更改加載程序源。

問題是,無論我在第2步和第3步之間花費多少時間,BusyIndi​​cator都沒有出現。

此外,當我註釋步驟3時,busyIndi​​cator顯示正確。

我做錯了什麼?

謝謝!

這是代碼:

Rectangle { 

    visible: true 
    width: 800 
    height: 480 
    signal handlerLoader (string name) 
    Loader { 
     id: pageLoader; 
     source: "init.qml"; 
    } 

    BusyIndicator { 
     id: busyIndicator_inicio 
     width: 100 
     height: 100 
     anchors.centerIn: parent 
     running: false 
    } 

    Connections { 
     target: pageLoader.item 
     onHandlerLoader: { 
      busyIndicator_inicio.running = true 
      pageLoader.source = name; 
     } 
    } 
} 

回答

3

的原因是,你的重負載Loader阻塞線程。 將其設置爲異步模式,以允許程序的其餘部分運行。 此外,我建議更喜歡聲明式綁定到處理程序中的命令式分配。見我的例子:

main.qml:

import QtQuick 2.4 
import QtQuick.Window 2.2 
import QtQuick.Controls 2.0 

Window { 
    width: 1000 
    height: 800 
    visible: true 

    Button { 
     text: 'load' 
     onClicked: { 
      loader.source = "TestObj.qml" 
     } 
    } 

    Loader { 
     anchors.fill: parent 
     id: loader 
     active: true 
     asynchronous: true 
     visible: status == Loader.Ready 
    } 

    BusyIndicator { 
     id: ind 
     anchors.fill: parent 
     running: loader.status == Loader.Loading 
    } 
} 

TestObj.qml:

import QtQuick 2.0 

Item { 
    Grid { 
     anchors.fill: parent 
     columns: width 
     rows: height 
     Repeater { 
      model: 100 
      Rectangle { 
       width: { for (var i = 0; i < 10000; i++) console.log(i); return 1 } 
       height: 1 
       color: 'green' 
      } 
     } 
    } 
} 

由於異步Loader可能顯示不完整的文件,有一段時間,我將它設置爲可見,只有當其status更改爲ready