December 28, 2011

kalster kalster
Lab Rat
315 posts

[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

3 replies

December 28, 2011

joonhwan joonhwan
Lab Rat
94 posts

assuming there is no duplicated tab name there, use

  1. int tabIndexFound = -1;
  2. for(int i=0; i<tab->count(); ++i) {
  3.     if(tabTextExpected == tab->tabText()) {
  4.         tabIndexFound = i;
  5.         break;
  6.     }
  7. }
  8. // tabIndexFound is the number you want?

 Signature 

joonhwan at gmail dot com

December 28, 2011

kalster kalster
Lab Rat
315 posts

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…

  1. if(tabTextExpected == tab->tabText()) {

should be something like…

  1. if(tabTextExpected == tab->tabText(i)) {

note the “i” in the above code.

thank you joonhwan

December 28, 2011

Andre Andre
Area 51 Engineer
6031 posts

Or just keep track of it yourself, by using a hash like this:

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

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

 
  ‹‹ Char* pointer returned by QString::toAscii().constdata() seems to be corrupted when QTextStream object is created      Unescaped backslashes are depreciated? ››

You must log in to post a reply. Not a member yet? Register here!