VRonin, thanks for looking at that. I'm seeing that -- call to d->makeVisible(index); -- also in Qt 5.5.1. As is apparent from the code, that "ensure visible" DOES work as long as SOME OTHER tab is currently current -- i.e. in the call to QTabBar::setCurrentIndex(int). Conversely, it doesn't work if the new current index is already current.
I regard this as a Qt bug. I've run into this a few times -- arguably overzealous optimizations in Qt code. Side effects like this cannot always be skipped. Here is the current Qt 5.5.1 code:
A workaround I've tested is as follows. If the QTabBar's current tab IS the one you want current (but may have been scrolled out of view by the user), you need to first call QTabBar::setCurrentIndex(int) with some other tab, and then call it again with the desired tab.
I did this (not generalized here, specific to my context) ...
// Note [Phil, RW 7.1, Qt 5.5.1, 5-2017]: Because of a bug in QTabBar::
// setCurrentIndex(int), the QTabBar isn't scrolled to the specified
// index IF that index is already the QTabBar's current index.
// SEE forum post: https://forum.qt.io/topic/79200/
if ((_objTabBar->currentIndex() == objTabInx) && (_objTabBar->count() > 1))
_objTabBar->setCurrentIndex ((objTabInx > 0) ? objTabInx-1 : 1);
That's definitely a bug. There seems to be a bug report about it QTBUG-15203, but it has been closed due to Qt4 EOL. Since it's still valid please report it for Qt5.
As for a workaround I'm afraid you will need to paint the tabs yourself. If you don't wan't to subclass and override paintEvent you can install an event filter and handle the painting there. Other that that I can't think of any other solution, apart maybe from adding some spaces to the front of the strings :P (just kidding, don't do it).
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.