qtquick: tabAt(index) now supports non-QtQuickControls TabBar

The C++ had hardcoded looking for the list view. User can now
override and write their own function to get to their custom
tabbar.
This commit is contained in:
Sergio Martins
2022-07-17 23:53:40 +01:00
parent b8acc8614c
commit 92d4922077
3 changed files with 68 additions and 38 deletions

View File

@@ -142,8 +142,16 @@ bool TabBar_qtquick::event(QEvent *ev)
QQuickItem *TabBar_qtquick::tabAt(int index) const
{
const QHash<int, QQuickItem *> 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<QQuickItem *>();
qWarning() << Q_FUNC_INFO << "Could not find tab for index" << index;
return nullptr;
}
QQuickItem *TabBar_qtquick::listView() const

View File

@@ -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: {

View File

@@ -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!");
}
}