diff --git a/src/qtquick/views/TabBar_qtquick.cpp b/src/qtquick/views/TabBar_qtquick.cpp index 2ef69f53..cb4b97ed 100644 --- a/src/qtquick/views/TabBar_qtquick.cpp +++ b/src/qtquick/views/TabBar_qtquick.cpp @@ -142,8 +142,16 @@ bool TabBar_qtquick::event(QEvent *ev) QQuickItem *TabBar_qtquick::tabAt(int index) const { - const QHash tabs = qmlTabs(); - return tabs.value(index, nullptr); + QVariant result; + const bool res = QMetaObject::invokeMethod(m_tabBarQmlItem, "getTabAtIndex", + Q_RETURN_ARG(QVariant, result), + Q_ARG(QVariant, index)); + + if (res) + return result.value(); + + qWarning() << Q_FUNC_INFO << "Could not find tab for index" << index; + return nullptr; } QQuickItem *TabBar_qtquick::listView() const diff --git a/src/qtquick/views/qml/TabBar.qml b/src/qtquick/views/qml/TabBar.qml index de1d6923..aab607b8 100644 --- a/src/qtquick/views/qml/TabBar.qml +++ b/src/qtquick/views/qml/TabBar.qml @@ -16,6 +16,21 @@ import QtQuick.Controls 2.9 TabBarBase { id: root + function getTabAtIndex(index) { + + // Iterate the internal ListView's children to find our tab + for(var i = 0; i < tabBar.children.length; ++i) { + var isListView = tabBar.children[i].toString().startsWith("QQuickListView"); + if (isListView) { + var listView = tabBar.children[i]; + if (index < listView.children.length) + return listView.children[0].children[index]; + } + } + + return null; + } + implicitHeight: tabBar.implicitHeight onCurrentTabIndexChanged: { diff --git a/src/qtquick/views/qml/TabBarBase.qml b/src/qtquick/views/qml/TabBarBase.qml index a2e22169..29938648 100644 --- a/src/qtquick/views/qml/TabBarBase.qml +++ b/src/qtquick/views/qml/TabBarBase.qml @@ -13,51 +13,58 @@ import QtQuick 2.9 import QtQuick.Controls 2.9 Item { - id: root + id: root - readonly property bool hasCustomMouseEventRedirector: parent.hasCustomMouseEventRedirector + readonly property bool hasCustomMouseEventRedirector: parent.hasCustomMouseEventRedirector - /// This is our C++ Group_qtquick.cpp - readonly property QtObject groupCpp: parent.groupCpp + /// This is our C++ Group_qtquick.cpp + readonly property QtObject groupCpp: parent.groupCpp - /// This is our C++ TabBar_qtquick.cpp - readonly property QtObject tabBarCpp: groupCpp ? groupCpp.tabWidget.tabBar : null + /// This is our C++ TabBar_qtquick.cpp + readonly property QtObject tabBarCpp: groupCpp ? groupCpp.tabWidget.tabBar : null - /// The number of tabs - readonly property int count: groupCpp ? groupCpp.tabWidget.dockWidgetModel.count : 0 + /// The number of tabs + readonly property int count: groupCpp ? groupCpp.tabWidget.dockWidgetModel.count : 0 - property int currentTabIndex: -1 + property int currentTabIndex: -1 - /// Don't override in custom TabBar's. - visible: tabBarCpp ? (tabBarCpp.tabBarAutoHide ? count > 1 : true) : count > 1 + /// Don't override in custom TabBar's. + visible: tabBarCpp ? (tabBarCpp.tabBarAutoHide ? count > 1 : true) : count > 1 - /// Don't override in custom TabBar's. Change implicitHeight instead. - height: visible ? implicitHeight : 0 + /// Don't override in custom TabBar's. Change implicitHeight instead. + height: visible ? implicitHeight : 0 - /// Feel free to customize - width: parent ? parent.width : 0 + /// Feel free to customize + width: parent ? parent.width : 0 - onCurrentTabIndexChanged: { - // Tells the C++ backend that the current dock widget has changed - if (groupCpp) - groupCpp.tabWidget.setCurrentDockWidget(currentTabIndex); - } + onCurrentTabIndexChanged: { + // Tells the C++ backend that the current dock widget has changed + if (groupCpp) + groupCpp.tabWidget.setCurrentDockWidget(currentTabIndex); + } - // If the currentIndex changes in the C++ backend then update it here - Connections { - target: root.groupCpp - function onCurrentIndexChanged() { - root.currentTabIndex = groupCpp.currentIndex; - } - } - - onTabBarCppChanged: { - if (tabBarCpp) { - if (!root.hasCustomMouseEventRedirector) - tabBarCpp.redirectMouseEvents(dragMouseArea) - - // Setting just so the unit-tests can access the buttons - tabBarCpp.tabBarQmlItem = this; - } + // If the currentIndex changes in the C++ backend then update it here + Connections { + target: root.groupCpp + function onCurrentIndexChanged() { + root.currentTabIndex = groupCpp.currentIndex; } } + + onTabBarCppChanged: { + if (tabBarCpp) { + if (!root.hasCustomMouseEventRedirector) + tabBarCpp.redirectMouseEvents(dragMouseArea) + + // Setting just so the unit-tests can access the buttons + tabBarCpp.tabBarQmlItem = this; + } + } + + /// Returns the QQuickItem* that implements the Tab + /// This is called by C++ and needs to be implemented in the derived class. + /// See TabBar.qml for an example. + function getTabAtIndex(index) { + console.warn("Override this function in the actual derived tab bar!"); + } +}