Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] how to get tab index when only text is known?
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 6.3k 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.
  • K Offline
    K Offline
    kalster
    wrote on last edited by
    #1

    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
    0
    • J Offline
      J Offline
      joonhwan
      wrote on last edited by
      #2

      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
      0
      • K Offline
        K Offline
        kalster
        wrote on last edited by
        #3

        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
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          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
          0

          • Login

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