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



  • 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



  • 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?
    @



  • 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



  • 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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.