Maya Pyside2 query selected tab

Hello,
I’m currently writing a UI in Maya using Pyside2. This UI has a number of tabs. Each Tab having 3 child tabs of model names within the tabs listWidget. I’m having a problem to query the active tab. I use the self.mytab.currentWidget() method that returns a pointer (PySide2.QtWidgets.QWidget(0x2439df45d30) at 0x0000024378A0B688) which I store in a variable. But how do I use this pointer to access the selected tabs name?

Here is some code to shed some light:

        #  self.tab_piece is a QtWidgets.QTabWidget() that has a number of tabs with child tabs that will 
        # be  queried for selected.
        #  Get selected pieces tab
        index = self.tab_pieces.currentIndex()

        tab_text = self.tab_pieces.tabText(index)
        # The above will only give me the parent name and not the active child tab name

        current_tab = self.tab_pieces.currentWidget()
        # the above returns this PySide2.QtWidgets.QWidget(0x2439df45d30) at 0x0000024378A0B688
        #  how to I query current_tab tab name?



You could traverse through children of that widget, and find your tabs among them, and do same logic - check for currentIndex, and get tabText (tab label) at that index. Something like this:

tab = QtWidgets.QTabWidget()  # parent tab, tabs_pieces in your snippet
widget = tab.currentWidget()
children = widget.children()  # children widgets

for child in children:
    if isinstance(child, PySide2.QtWidgets.QTabBar):  # is it QTabBar, since there will be other widgets
        current_index = child.currentIndex()  # get current tab's index
        tab_text = child.tabText(current_index)  # get label of current tab

P.S. If your children tab widgets aren’t QTabBar, replace instance checking with correct type.
Or avoid ‘if isinstance(…):’ check altogether, and wrap <current_index, tab_text> in try-except block.

2 Likes

Hey bjeDark, thanks for pointing me in the right direction. I got it working! :slightly_smiling_face:

I used QTabWidget instead of QTabBar.

Her is a snippet of the code to help others. Note: I was querying the active tab from a parent tab that had 3 child tabs to which one of these will be active:

        #  Get selected parent tab and it's selected tab category name.
        #  self.tab_pieces is my parent tab
        tab_pieces_index = self.tab_pieces.currentIndex()
        tab_pieces_text = self.tab_pieces.tabText(tab_pieces_index)  # Get tab category name
        print(f"Tab Pieces category name: {tab_pieces_text}")
        current_tab = self.tab_pieces.currentWidget()

        children = current_tab.children()  # Get parent tab children, list pointer to widgets.
        child_tab_text = ""

        # Find tab widget within children. This will get the active tab!
        for child in children:
            if isinstance(child, QtWidgets.QTabWidget):
                current_index = child.currentIndex()  # get current tab's index
                child_tab_text = child.tabText(current_index)  # get label of current tab

        print(f"Current active tab: {tab_pieces_text}_{child_tab_text}")

Thanks for your help. :+1:

Rather than iterating over all children and testing type with isinstance better to use the built in Qt way of finding child objects by type.

Modifying your last example:

current_tab = self.tab_pieces.currentWidget()
children = current_tab.findChildren(QtWidgets.QTabWidget)
for child in children:
    ...  # continue example

The methods findChild and findChildren exist on QObject and allow finding by type (and optionally also by name or pattern).
https://doc.qt.io/qtforpython-5/PySide2/QtCore/QObject.html#PySide2.QtCore.PySide2.QtCore.QObject.findChild

2 Likes

Hey ross-g,

Nice tip! That will come in very handy. :slightly_smiling_face:

Really appreciate that. Thanks