【问题标题】:QML BusyIndicator while loading a heavy qml file加载繁重的 qml 文件时的 QML BusyIndi​​cator
【发布时间】:2016-11-08 10:01:40
【问题描述】:

我在加载 qml 文件 (http://doc.qt.io/qt-5/qml-qtquick-loader.html) 时尝试运行 BusyIndi​​cator (http://doc.qt.io/qt-5/qml-qtquick-controls-busyindicator.html),但 BusyIndi​​cator 没有出现。

我想做的是: 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;
        }
    }
}

【问题讨论】:

    标签: qt qml loader busyindicator


    【解决方案1】:

    原因是,您的重载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 时才可见。

    【讨论】:

      猜你喜欢
      • 2021-12-12
      • 1970-01-01
      • 1970-01-01
      • 2012-04-30
      • 1970-01-01
      • 2017-07-18
      • 1970-01-01
      • 2018-10-02
      • 1970-01-01
      相关资源
      最近更新 更多