c++ - QT 在 StackView 中排列组件

标签 c++ qt qml stackview

我们正在开发QT跨平台应用程序。应用程序有许多功能屏幕和导航。为了简化屏幕处理,我们使用 QT QML StackView 。推送到 StackView 的每个项目都是 Page 。我正在使用以下函数来推送和弹出屏幕。

stackview.push(someurl); //push new url

stackview.pop(); //pop current URL or page

我的问题是我们如何重用已经推送到堆栈的组件/页面?

示例:我有以下屏幕 A、B、C、D。堆栈看起来像 A->B->C->D。现在,我想从屏幕 D 导航到屏幕 A。由于应用程序的统一架构,我不想执行 3 次 pop(),相反,应用程序应该重用已推送的屏幕 A 并将其带入到达顶点。

我们可以简单地将屏幕 A 推送到 StackView (A->B->C->D->A),但是我们将不同的信号/槽连接到每个屏幕,并且它每次推送屏幕时都进行连接并不是一个好习惯,也不想增加应用程序 StackView 的大小。

在这种情况下最好使用什么方法?在这种情况下我可以使用任何其他 QT 组件/控件,以便我可以根据索引渲染页面?

我们尝试提前创建组件 qml Property并根据 URL 进行推送。它在示例应用程序中非常有用。但在我们的应用程序中,QML 组件数据紧密依赖于 C++ 数据(或运行时的一些计算)。如果我们提前创建组件,应用程序会导致崩溃。所以我们不愿意使用这种方法。

property Page page1: Screen1{} //Screen1.qml 
property Page page2: Screen2{} //Screen2.qml 
property Page page3: Screen3{} //Screen3.qml 
property Page page4: Screen4{} //Screen4.qml 

是否可以基于 Index 来加载 Stackview 组件,我在文档中没有找到与此相关的任何内容。

最佳答案

Due to uniform architecture of the application i don't want to do 3 times pop(), instead application should reuse already pushed Screen A and bring it to top.

您不必调用 pop() 三次即可返回 A。根据 documentation :

Item pop(Item item = undefined) [...]

item: if specified, all items down to (but not including) item will be popped off. If item is null, all items down to (but not including) the first item will be popped. If not specified, only the current item will be popped.

仅调用一次 pop(null) 即可将堆栈移至第一个压入的 A。

另一种可能的方法是使用 StackLayout允许您基于索引显示 QML 组件。

StackLayout {
    id: layout

    Screen1 {}
    Screen2 {}
    Screen3 {}
    Screen4 {}
}

然后您可以通过更改currentIndex来选择显示哪个屏幕。 property : layout.currentIndex = 1//显示Screen2

关于c++ - QT 在 StackView 中排列组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45954482/

相关文章:

c# - 在数据迁移中传输大数据

c++ - 从参数包中获取 typedef

c++ - 集成 C 和 C++ 程序

c++ - Qt 使用自定义 QObject 类型调用 qml 函数

c++ - 使用 typedef 类型作为类模板参数

c++ - Qt 更新 QGraphicsScene 中的 QPixmapItem

C++(Qt5.5)通过保存功能扩展图片查看器实例

c++ - 遍历作为 QList<int> 的 QVariant?

javascript - QML : how to pass javascript function as argument in another function