Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. SwipeView and TabBar currentIndex issue when dynamic object creation
Forum Update on Monday, May 27th 2025

SwipeView and TabBar currentIndex issue when dynamic object creation

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 476 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nsourl
    wrote on last edited by
    #1

    Hi,

    I am using Qt 5.15.0 and trying to load some tabs dynamically on SwipeView before using it on my project. But unfortunately, I am facing an issue with currentIndex of SwipeView and TabBar that it doesn't increment at all. So, the Rectangle area will remain always red instead of switching colors between red and green.

    Am I doing something wrong with my QML example? Am I missing something how to connect SwipeView and TabBar? Please check my QML example below.

    Thanks!

    import QtQuick 2.12
    import QtQuick.Controls 2.12
    
    ApplicationWindow {
        width: 640
        height: 480
        visible: true
        title: qsTr("Tabs")
    
        Component {
            id: resultListView
            Rectangle {
                color: "blue"
            }
        }
    
        Component {
            id: tabButton
    
            TabButton {
                width: implicitWidth
            }
        }
    
        SwipeView {
            id: swipeView
            anchors.fill: parent
            currentIndex: tabBar.currentIndex
        }
    
        footer: TabBar {
            id: tabBar
            currentIndex: swipeView.currentIndex
        }
    
        Component.onCompleted: {
            for (var i = 0; i < 5; i++) {
                console.log("tabBar currentIndex: ", tabBar.currentIndex)
                console.log("stack currentIndex: ", swipeView.currentIndex)
    
                // Add TabButtons dynamically on TabBar
                tabBar.addItem(tabButton.createObject(tabBar, {text: qsTr("tab - " + (tabBar.count))} ) )
    
                var col = "green";
                if (tabBar.currentIndex % 2 === 0) {
                    col = "red";
                }
    
                console.log("tabBar color: ", col)
    
                swipeView.addItem(resultListView.createObject(swipeView, {color: col}))
            }
        }
    }
    
    1 Reply Last reply
    0
    • ndiasN Offline
      ndiasN Offline
      ndias
      wrote on last edited by
      #2

      Hi @nsourl ,

      Please consider using count (total number of items) instead of currentIndex property:

                  if (tabBar.count % 2 === 0) {
                      col = "red";
                  }
      

      Regards

      1 Reply Last reply
      1
      • N Offline
        N Offline
        nsourl
        wrote on last edited by nsourl
        #3

        Hi @ndias ,

        Indeed, using count instead of currentIndex solved my issue.

        Thanks!

        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved