QTabBar: Getting list of indexes of visible tabs
-
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).
-
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); }
-
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()
tocontains()
. -
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?
-
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.