0

我正在尝试将选项卡从不同的文件动态加载到 TabView 中。为此,我使用 Qt.createComponent 并将组件添加到视图中。该选项卡已加载,但其内容未显示并且已正确加载。像这样:

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
        }
    }
}

我做错了什么?

4

1 回答 1

0

在阅读了@folibis 清理建议后,我意识到我得到Tab onCompleted处理程序的方式不起作用。为什么我不知道。然而更换

var c = Qt.createComponent("tab.qml");
editor.addTab("tab", c);
var last = editor.count - 1;
editor.getTab(last).active = true;

var c = Qt.createComponent("tab.qml");
var tab = editor.addTab("tab", c);
tab.active = true

解决了。

于 2015-12-21T23:53:05.507 回答