如何中止加载程序中的加载组件?
How to abort loading component in Loader?
我有一个 Loader
对象,它加载了一些非常重的组件。某些事件在加载过程中到达,需要加载停止并返回清空 Loader
。可能吗?
我不知道你的问题是什么,那些 在加载程序完成之前被销毁的对象 ,但也许问题就在那里?如果没有,这应该有效:
如果没有帮助,请在您的问题中添加一些代码,以重现您的问题。
main.qml
import QtQuick 2.7
import QtQuick.Controls 2.0
ApplicationWindow {
id: root
visible: true
width: 400; height: 450
Button {
text: (complexLoader.active ? 'Loading' : 'Unloading')
onClicked: complexLoader.active = !complexLoader.active
}
Loader {
id: complexLoader
y: 50
width: 400
height: 400
source: 'ComplexComponent.qml'
asynchronous: true
active: false
// visible: status === 1
}
BusyIndicator {
anchors.fill: complexLoader
running: complexLoader.status === 2
visible: running
}
}
ComplexComponent.qml
import QtQuick 2.0
Rectangle {
id: root
width: 400
height: 400
Grid {
id: grid
anchors.fill: parent
rows: 50
columns: 50
Repeater {
model: parent.rows * parent.columns
delegate: Rectangle {
width: root.width / grid.columns
height: root.height / grid.rows
color: Qt.rgba(Math.random(index),
Math.random(index),
Math.random(index),
Math.random(index))
}
}
}
}
中止对象创建
正如 Qt 所记录的那样,unload/abort 对象实例化存在三种方法:
- 将
Loader.active
设置为false
- 将
Loader.source
设置为空字符串
- 将
Loader.sourceComponent
设置为undefined
异步行为
为了能够在加载期间更改这些属性,如果场景包含 QQuickWindow
。
,Loader.asynchronous
should be true
, otherwise the GUI thread is busy with loading the object. You also need to QQmlIncubationController
for your QQmlEngine
to control the idle time used for object incubation. Without such a controller Loader.asynchronous
does not have any effect. Note that QQmlApplicationEngine
会自动安装默认控制器
错误
直到上次测试的Qt版本(Qt 5.8.0,5.9.0 beta),在中止未完成的对象孵化时存在严重的内存泄漏(至少在某些情况下,包括derM答案中的示例) 导致大型组件的内存使用量快速增加。 bug report 已创建,其中包含建议的解决方案。
我有一个 Loader
对象,它加载了一些非常重的组件。某些事件在加载过程中到达,需要加载停止并返回清空 Loader
。可能吗?
我不知道你的问题是什么,那些 在加载程序完成之前被销毁的对象 ,但也许问题就在那里?如果没有,这应该有效: 如果没有帮助,请在您的问题中添加一些代码,以重现您的问题。
main.qml
import QtQuick 2.7
import QtQuick.Controls 2.0
ApplicationWindow {
id: root
visible: true
width: 400; height: 450
Button {
text: (complexLoader.active ? 'Loading' : 'Unloading')
onClicked: complexLoader.active = !complexLoader.active
}
Loader {
id: complexLoader
y: 50
width: 400
height: 400
source: 'ComplexComponent.qml'
asynchronous: true
active: false
// visible: status === 1
}
BusyIndicator {
anchors.fill: complexLoader
running: complexLoader.status === 2
visible: running
}
}
ComplexComponent.qml
import QtQuick 2.0
Rectangle {
id: root
width: 400
height: 400
Grid {
id: grid
anchors.fill: parent
rows: 50
columns: 50
Repeater {
model: parent.rows * parent.columns
delegate: Rectangle {
width: root.width / grid.columns
height: root.height / grid.rows
color: Qt.rgba(Math.random(index),
Math.random(index),
Math.random(index),
Math.random(index))
}
}
}
}
中止对象创建
正如 Qt 所记录的那样,unload/abort 对象实例化存在三种方法:
- 将
Loader.active
设置为false
- 将
Loader.source
设置为空字符串 - 将
Loader.sourceComponent
设置为undefined
异步行为
为了能够在加载期间更改这些属性,如果场景包含 QQuickWindow
。
Loader.asynchronous
should be true
, otherwise the GUI thread is busy with loading the object. You also need to QQmlIncubationController
for your QQmlEngine
to control the idle time used for object incubation. Without such a controller Loader.asynchronous
does not have any effect. Note that QQmlApplicationEngine
会自动安装默认控制器
错误
直到上次测试的Qt版本(Qt 5.8.0,5.9.0 beta),在中止未完成的对象孵化时存在严重的内存泄漏(至少在某些情况下,包括derM答案中的示例) 导致大型组件的内存使用量快速增加。 bug report 已创建,其中包含建议的解决方案。