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. QTabBar: Getting list of indexes of visible tabs

QTabBar: Getting list of indexes of visible tabs

Scheduled Pinned Locked Moved General and Desktop
qwidgetqtabbarqtabwidget
5 Posts 3 Posters 3.1k 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.
  • Zee_07Z Offline
    Zee_07Z Offline
    Zee_07
    wrote on last edited by Zee_07
    #1

    How can i get a list of all visible tab index in current view of QTabBar. Suppose my tab bar has 100 tabs (UsesScrollButtons is true so size of QTabBar is fixed) and only 51 to 57 index is visible in current window size. Then how can I get the list of all these 6 visible tabs indexes. like (51,52,....57).

    Thanks and Kind Regards,
    Zee_07

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      Seems to be no easy way but if you can live with also partial shown tabs are considered
      visible then this might do the trick. :)

      QVector<int> GetVisibleTabs( QTabBar* b )
      {
          QVector<int> hits;
          int pxto = b->width();
          int y = 10;
          int step = 0;
          for (int i = 0; i <  pxto; i += step  ) {
              QPoint pos(i, y);
              int index =  b->tabAt( pos  );
              hits.append(index);
              step = b->tabRect(index).width();
          }
      
          return hits;
      
      }
      // just test
      void MainWindow::on_getvisi_clicked()
      {
      
      QVector<int>  mylist=GetVisibleTabs(b);
      for (int c=0; c<mylist.size();c++)
          qDebug() << mylist.at(c);
      
      }
      
      1 Reply Last reply
      1
      • Chris KawaC Offline
        Chris KawaC Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by Chris Kawa
        #3

        While @mrjj suggestion works it is slightly inefficient (tabAt() is a loop).
        It also doesn't take into account possible top padding (y > 10) and spins on testing points that are not occupied by tabs (possible left padding).

        Here's an alternative approach with lower number of lookups:

        QRect r = tabBar->rect();
        int numTabs = tabBar->count();
        int firstVisible = 0;
        int lastVisible = numTabs - 1;
        
        while(firstVisible < numTabs && !r.intersects(tabBar->tabRect(firstVisible)))
            ++firstVisible;
        
        while(lastVisible > 0 && !r.intersects(tabBar->tabRect(lastVisible)))
            --lastVisible;
        
        qDebug() << firstVisible << lastVisible;
        

        If you want only fully visible tabs switch intersects() to contains().

        1 Reply Last reply
        1
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Sweet Chris.
          Much smarter if many tabs visible and also possible to only get fully visible.

          I forgot to ask Zee.

          Why you need to know this?

          1 Reply Last reply
          1
          • Zee_07Z Offline
            Zee_07Z Offline
            Zee_07
            wrote on last edited by
            #5

            Thanks mrjj and Chris Kawa for your valuable response.
            Actually I want to create my own customized scroll-button for tabBar with either end button( two buttons like << and >> including qt already provided scroll-buttons < and >) So that we can directly go to the fist or last tab. In Qt scroll-button for Qtabbar provides only step forward or backward functionality means we can scroll only one tab at a time.
            Suppose my QTabBar contains 100 tab and currently visible tabs are 55,56,and 57 and if we want to go on first then we have to click approx 49 times or stay in clicked state for few seconds.

            if you guys have any approach for doing this efficiently then please. It would be highly appreciated.

            Thanks and Kind Regards,
            Zee_07

            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