Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    [SOLVED] how to get tab index when only text is known?

    General and Desktop
    3
    4
    6095
    Loading More Posts
    • 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.
    • K
      kalster last edited by

      I have created several tabs. when the user clicks a tab, how to get the index number of that tab when only the Text of that tab is known. I would like to store the results in a QString.

      if there is no way of finding the index of a tab Text, then i guess i can store the tab Text in an array and search the array

      1 Reply Last reply Reply Quote 0
      • J
        joonhwan last edited by

        assuming there is no duplicated tab name there, use

        @
        int tabIndexFound = -1;
        for(int i=0; i<tab->count(); ++i) {
        if(tabTextExpected == tab->tabText()) {
        tabIndexFound = i;
        break;
        }
        }
        // tabIndexFound is the number you want?
        @

        joonhwan at gmail dot com

        1 Reply Last reply Reply Quote 0
        • K
          kalster last edited by

          yes the code will work. Gee i should have thought of that.

          for anyone else reading this topic that has the same problem, note the code below...

          @if(tabTextExpected == tab->tabText()) {@

          should be something like...

          @if(tabTextExpected == tab->tabText(i)) {@

          note the "i" in the above code.

          thank you joonhwan

          1 Reply Last reply Reply Quote 0
          • A
            andre last edited by

            Or just keep track of it yourself, by using a hash like this:
            @
            QHash<QString, int> m_tabIndices;
            @

            However, because it is unlikely you will have many tabs, I guess a linear search works just as well if not better. Still, in general, you should remember it does not scale very well.

            1 Reply Last reply Reply Quote 0
            • First post
              Last post