Get tab status using pyforms

184 Views Asked by At

I'm working with pyforms to create a tab widget and I want to get and set the current active tab. Consider this example:

self.formset = [{ 
    'Person A': ['_firstname', '_lastname'],
    'Person B': ['_firstname', '_lastname'] }]

so we get 2 tabs Person A and Person B. If I switch between them I would like to be informed with something similar to PyQt function currentIndex(). So far I havn't been able to find a method in the pyforms documentation, is there a way to accomplish this just using pyforms?

1

There are 1 best solutions below

0
On

The main issue in getting or setting the current index of your tab widget is to get access to the QTabWidget created by pyforms when the layout is generated. Once you have access to it, you simply call the setCurrentIndex(int)/currentIndex() of the widget.
A (dirty) quick fix to this is to modify the BaseWidget.py located in the pyforms module files which can be <VIRTUALENV_DIR>/lib/python3.6/site-packages/pyforms/gui when using virtualenv.

def generate_tabs(self, formsetdict):
    """
    Generate QTabWidget for the module form
    @param formset: Tab form configuration
    @type formset: dict
    """
    tabs = QTabWidget(self)
    for key, item in sorted(formsetdict.items()):
        ctrl = self.generate_panel(item)
        tabs.addTab(ctrl, key[key.find(':') + 1:])
    self.tabs = tabs
    return tabs

Note the additional :

self.tabs = tabs

Then in the code of your widget/app (subclass of BasicWidget) :

>>> _t = self.tabs
>>> _t.setCurrentIndex(3) # activate the 4th tab
>>> print(_t.currentIndex())
3