How to set current tab of QTabWidget by name?

aoh picture aoh · Aug 23, 2017 · Viewed 15k times · Source

PyQt5's QTabWidget has a method setCurrentIndex that you can use to get a particular tab to become the active tab. I can't seem to find any way to index by tab name though (which I set in Qt Designer). Is there any way (either direct or indirect) to index by name instead?

http://doc.qt.io/qt-5/qtabwidget.html#currentIndex-prop

Answer

ekhumoro picture ekhumoro · Aug 23, 2017

The tab name becomes the object-name of the widget set as the tab's page. When the tab is added, the page will be automatically re-parented to the internal stack-widget of the tab-widget. This means you can get a reference to the page like this:

page = tabwidget.findChild(QWidget, tabname)

and get its index like this:

index = tabwidget.indexOf(page)

or set the current tab directly by name like this:

tabwidget.setCurrentWidget(tabwidget.findChild(QWidget, tabname))