gpt4 book ai didi

qt - 如何以编程方式创建选项卡的项目?

转载 作者:行者123 更新时间:2023-12-04 12:58:34 25 4
gpt4 key购买 nike

我动态地将选项卡添加到 TabView 并将选项卡的项目传递给 c++ 以进行进一步处理。问题是方法 tabview.getTab(tabview.getTab(tabview.count-1).item) 为索引 >0 返回 null。这是代码:

//main.qml
import QtQuick 2.2
import QtQuick.Controls 1.1
import QtQuick.Layouts 1.1

ApplicationWindow {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
signal tabAdded(variant c)
ColumnLayout{
anchors.fill: parent
TabView{
visible: true
id:tabview
Layout.fillHeight: true
Layout.fillWidth: true
}
Button{
text: "add tab"
onClicked:{
var c = Qt.createComponent("Tab.qml");
tabview.addTab("tab", c)
// tabAdded(tabview.getTab(tabview.count-1).item)
console.log(tabview.getTab(tabview.count-1).item)
}
}
}

}

//Tab.qml
import QtQuick 2.0
import QtQuick.Controls 1.1

Item{
signal tabButtonClicked()
anchors.fill: parent
Button{
text: "tabButton"
anchors.centerIn: parent
onClicked: tabButtonClicked()
}

}

我发现如果手动激活选项卡(索引)(通过在其上单击鼠标),tabview.getTab(index).item 会返回适当的值。似乎仅当用户首次激活选项卡时才创建选项卡的项目。那么,如何让item在tab创建后立即创建呢?

最佳答案

TableView 中的每个选项卡都由继承自 Loader componentTab 组件表示。 .如果你想强制标签加载它的内容,只需设置 active property为真。此属性控制何时必须加载组件。所以现在,您的按钮代码如下所示:

Button{
text: "add tab"
onClicked:{
var c = Qt.createComponent("Tab.qml");
tabview.addTab("tab", c);
var last = tabview.count-1;
tabview.getTab(last).active = true; // Now the content will be loaded
console.log(tabview.getTab(last).item);
}

希望对你有帮助

关于qt - 如何以编程方式创建选项卡的项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27103028/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com