gpt4 book ai didi

c++ - 转到特定页面后,如何将SwipeView的currentIndex设置为TabBar的currentIndex "by reference"?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:22:04 26 4
gpt4 key购买 nike

我开始使用 QtQuick Controls 2.0。我有使用 C++ 的经验和少量的 Qt 经验,但我以前没有使用过 QML。

我有一个 TabBar 和一个 SwipeView 相互链接。我的意思是,当您在 TabBar 上选择一个页面时,SwipeView 会转到该页面。当您从 SwipeView 滑动到一个页面时,TabBar 会自行更新以反射(reflect)这一点。

作为学习练习,我决定创建一个将用户转到第二页的按钮。问题是我似乎无法找到一种方法来做到这一点而不会弄乱 TabBarSwipeView 之间的链接。

下面的代码是我想出的最好的。它正确转到第二页,当我使用 TabBar 更改当前页面时,SwipeView 仍会更新。但是,滑动 到新页面不再更新 TabBar。似乎将 tabBar.currentIndex 设置为 swipeView.currentIndex 只有在用冒号初始化时才具有按引用设置的效果。使用等号按值设置。我怎样才能移动到特定页面,同时仍然保持不变 swipeView.currentIndex == tabBar.currentIndex

// main.qml
import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.0

ApplicationWindow {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
SwipeView {
id: swipeView
anchors.fill: parent
currentIndex: tabBar.currentIndex

Page {
Button {
text: qsTr("Continue to Page 2")
onClicked: {
tabBar.currentIndex = 1;
// this next line merely sets tabBar.currentIndex to 1
tabBar.currentIndex = swipeView.currentIndex
}
anchors.centerIn: parent
width: text.implicitWidth
height: text.implicitHeight
}
}

Page {
Label {
text: qsTr("Second page")
anchors.centerIn: parent
}
}
}

footer: TabBar {
id: tabBar
currentIndex: swipeView.currentIndex
TabButton {
text: qsTr("First")
}
TabButton {
text: qsTr( "Second")
}
}
}

C++ 代码只是 Qt Creator 为我提供的默认代码:

// main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>

int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);

QQmlApplicationEngine engine;
engine.load(QUrl(QLatin1String("qrc:/main.qml")));

return app.exec();
}

最佳答案

为了在不破坏 currentIndex 绑定(bind)的情况下移动到下一页或上一页,您可以调用 incrementCurrentIndex()decrementCurrentIndex() , 分别。这些方法在 Qt 5.8 的 Qt Quick Controls 2.1 中引入。

鉴于 currentIndex setter aka。 QQuickContainer::setCurrentIndex()是一个slot,也可以从QML调用setCurrentIndex()跳转到任意页面。这也不会破坏现有的绑定(bind)。

Button {
onClicked: tabBar.setCurrentIndex(1)
}

关于c++ - 转到特定页面后,如何将SwipeView的currentIndex设置为TabBar的currentIndex "by reference"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43578012/

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