问题描述
当我尝试访问 Tab 元素的子元素时,如果它不是活动的,QML 会抛出一个错误,说是 undefined
.
When I try to access a son of a Tab element, if it is not active QML throws an error saying is undefined
.
main.qml
import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Window 2.0
ApplicationWindow {
TabView {
Tab {
id: mytab1
}
Tab {
id: myTab2
Rectangle {
//(...)
}
}
}
Connections {
target: eventManager
onData: {
var scene = myTab2.children[0];
console.log(scene);
}
}
}
因此,如果 myTab2 处于活动状态,我可以在控制台中看到 QQuickItem_QML_15(0x27f1e2e0)
.如果 myTab2 未激活,则 qml 将抛出 TypeError: Cannot read property 'children' of undefined
.
So, if myTab2 is active, I can see in the console QQuickItem_QML_15(0x27f1e2e0)
. If myTab2 is not active, then qml throws TypeError: Cannot read property 'children' of undefined
.
如果选项卡未处于活动状态,为什么未定义,我该如何解决?
Why is undefined if the tab is not active and how can I fix it?
谢谢!
推荐答案
来自 Qt 文档 网站,我找到了解决方案.
From the Qt documentation website, I've found a solution.
标签被延迟加载;仅已成为当前选项卡(对于例如,通过单击它们)将具有有效的内容.你可以强迫通过将 active 属性设置为 true 来加载标签:
import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Window 2.0
ApplicationWindow {
TabView {
Tab {
id: mytab1
active: true
}
Tab {
id: myTab2
active: true
Rectangle {
//(...)
}
}
}
Connections {
target: eventManager
onData: {
var scene = myTab2.children[0];
console.log(scene);
}
}
}
所以,我在两个选项卡中都添加了属性 active: true
,它工作正常!
So, I've added the property active: true
in both tabs, and It works fine!
这篇关于选项卡未处于活动状态时未定义的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!