我开始使用 QtQuick Controls 2.0。我有 C++ 的经验和少量的 Qt 经验,但我以前没有使用过 QML。
我有一个相互关联的 aTabBar
和 a 。SwipeView
我的意思是,当您在 上选择一个页面时TabBar
,将SwipeView
转到该页面。当您从 滑动到页面时SwipeView
,TabBar
更新本身会反映这一点。
作为学习练习,我决定创建一个按钮,将用户引导至第二页。问题是我似乎无法找到一种方法来做到这一点,而不会弄乱TabBar
和之间的链接SwipeView
。
以下代码是我想出的最好的。它正确地进入第二页,当我用静止更新更改当前页面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();
}