TransWikia.com

Listing active radio buttons within tab widget or layout?

Geographic Information Systems Asked by Nathan Leenaerts on August 9, 2020

I have a plugin widget with some tabs, and within the tabs a layout of radio buttons as shown below:

enter image description here

I would like to capture the list of active radio buttons within each tab like so:
enter image description here

Is there an easy way to do this dynamically for each tab instead of programatically capturing the state of each radio button?

here is the structure of the UI in QT designer if it helps:
enter image description here

One Answer

I found a solution which allows to parse the Tabs in a TabWidget and make a dictionnary of all the Radio buttons inside them:

# make a dictionary for each tab and the list of radio buttons in each one
self.dockwidget = Filtres_standards_PDSDockWidget()
TabWidget = self.dockwidget.FilterTabs.children()            
StackedWidgets = [SWidget for SWidget in  TabWidget if isinstance(SWidget,qw.QStackedWidget)][0]
TabsList = [Tab for Tab in StackedWidgets.children() if isinstance(Tab,qw.QWidget)]                
RadioButtonsLists={Tab.objectName() : [RadioButtons for RadioButtons in Tab.children() if isinstance(RadioButtons, qw.QRadioButton)] for Tab in TabsList}

then a fuction that checks which RButtons are checked to build a query:

#A function to check whithin each tab the list of checked radiobuttons and building a query from it   
def querybuilder(RadioButtonsLists):
    QueryText=''
    for RadioButtonsList in RadioButtonsLists:
        checkedOnRb = ''
        RBListlen = []
        for RadioButton in RadioButtonsLists[RadioButtonsList]:
            if RadioButton.isChecked():
                RBListlen.append(1)
                if len(checkedOnRb)==0:
                    checkedOnRb = RadioButton.text()
                else:
                    checkedOnRb += ", " + RadioButton.text()
        if len(RBListlen)>0:
            if len(QueryText)==0:
                QueryText = RadioButtonsList + ' in (' + str(checkedOnRb) + ')'
            else:
                QueryText += ' and ' + RadioButtonsList + ' in (' + str(checkedOnRb) + ')'
    return QueryText

def printquery(RadioButtonsLists):
    self.dockwidget.label.setText(str(querybuilder(RadioButtonsLists)))

the result:

enter image description here

Answered by Nathan Leenaerts on August 9, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP