【发布时间】:2019-09-05 10:59:28
【问题描述】:
我创建了以下 MWE (Qt 5.13.0):
import QtQuick 2.0
import QtQuick.Window 2.2
import QtQuick.Controls 2.3
ApplicationWindow
{
property int itemsNo: 3;
id: window
visible: true
width: 480
height: 480
SwipeView
{
anchors.fill: parent;
id: theSwipeView;
Loader
{
sourceComponent: theSingleComp;
Component
{
id: theSingleComp;
Page
{
Text
{
text: "The single one";
}
}
}
}
Repeater
{
model: itemsNo;
Loader
{
sourceComponent: theMultiComp;
Component
{
id: theMultiComp;
Page
{
Text
{
text: "The multi one " +
(theSwipeView.currentIndex - 1);
}
}
}
}
}
}
}
在我的程序中,我有一个独特的组件 (theSingleComp) 和他背后的多个组件 (theMultiComp)。至于现在,我需要实现以下功能:
如果用于theMultiComp 的模型只有一项,则只显示该项而不显示theSingleComp。如果有更多 theMultiComp 项目,请像现在一样显示。在我看来,如果我保持静态定义的项目,这是不可能工作的。但另一方面,我不知道如何动态执行此操作,因为在某些情况下,其中一个组件根本不应该显示。我尝试了这样的方法:
sourceComponent: (itemsNo > 1) ? theSingleComp : null;
但是这个空组件的页面仍然被创建。
【问题讨论】: