从不同文件加载标签不显示任何内容。 QML
问题描述:
我想从另一个文件中动态加载标签到TabView中。为此,我使用Qt.createComponent并将该组件添加到视图中。该选项卡已加载,但其内容不显示,并且已正确加载。就像这样:从不同文件加载标签不显示任何内容。 QML
TabView {
id:editor
Layout.minimumWidth: 50
Layout.fillWidth: true
}
Component.onCompleted: {
function newTab() {
var c = Qt.createComponent("tab.qml");
editor.addTab("tab", c);
var last = editor.count - 1;
editor.getTab(last).active = true;
}
newTab();
newTab();
}
与 “tab.qml” 文件:
import QtQuick 2.0
import QtQuick.Layouts 1.1
import QtQuick.Controls 1.4
Tab {
Rectangle {
Layout.fillWidth: true
Layout.fillHeight: true
color: "lightgray"
TextArea {
anchors.fill: parent
}
}
}
我该怎么办错了吗?
答
阅读@folibis清理建议后,我意识到我得到Tab
onCompleted
处理程序无法正常工作的方式。为什么我不知道。然而,随着
var c = Qt.createComponent("tab.qml");
var tab = editor.addTab("tab", c);
tab.active = true
更换
var c = Qt.createComponent("tab.qml");
editor.addTab("tab", c);
var last = editor.count - 1;
editor.getTab(last).active = true;
解决它。