Reputation: 33
I'm dynamically creating UI elements in layers of a QStackedLayout widget. I have created a generator that stores the objects for a targeted stacked widget layer so that I can collect settings made with UI elements contained in the layer when needed. I'm certain that I have isolated a radio button, but I do not know how to access the 'isChecked()' property. If I have this item isolated and stored as a variable using this line ('items' is the Generator):
target_radio_button = items.__next__()
how can I get to the 'isChecked' property to query whether or not it is checked, knowing that this QWidgetItem is a QRadioButton? If I print the type of the isolated object using:
print (type(target_radio_button))
This is what is returned:
<class 'PySide2.QtWidgets.QWidgetItem'>
Here is the minimal code requested:
from PySide2 import QtWidgets
import sys
class Test(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Test, self).__init__(parent)
self.setWindowTitle("Test")
self.setGeometry(50, 50, 200, 200)
self.test_layout = QtWidgets.QVBoxLayout()
self.setLayout(self.test_layout)
self.main_stacked_layout = QtWidgets.QStackedLayout()
self.test_layout.addLayout(self.main_stacked_layout)
self.widget = QtWidgets.QWidget()
self.widget_layout = QtWidgets.QVBoxLayout()
self.widget.setLayout(self.widget_layout)
self.radio = QtWidgets.QRadioButton('Sample Radio Button')
self.h_layout = QtWidgets.QHBoxLayout()
self.h_layout.addWidget(self.radio)
self.widget_layout.addLayout(self.h_layout)
self.layout_container = [self.widget]
self.main_stacked_layout.addWidget(self.widget)
self.demo()
def demo(self):
target_widget = self.layout_container[0]
target_layout = target_widget.layout()
for layout_item in target_layout.findChildren(QtWidgets.QHBoxLayout):
items = (layout_item.itemAt(i) for i in range(layout_item.count()))
self.get_settings_value(items, 'Boolean')
# for item in target_layout.findChildren(QtWidgets.QRadioButton):
# print (item.text())
# print (item.isChecked())
def get_settings_value(self, items, value_type):
if value_type == 'Boolean':
target_radio_button = items.__next__()
print (type(target_radio_button))
#print (target_radio_button.isChecked())
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
form = Test()
form.show()
sys.exit(app.exec_())
There are two areas I commented out- the first block I disabled demonstrates a way that I can see to get the information I'm looking for, but its not versatile enough. What I need is to capture the children in the target layout without filtering for a specific type (in the case of the sample code this would be for a QRadioButton). I want to send that group of found children to the simplified "get_settings_value" function, and from there break out instructions on what to look for. I intend to have multiple different "value_type" arguments to extract values depending on the UI widget. The second commented out area is the print statement checking if the element 'isChecked()'. This throws the error, and I'm trying to understand how to gain access to this property.
Upvotes: 0
Views: 66
Reputation: 244311
The itemAt()
method returns a QLayoutItem
that is a generic container and its derived classes are QLayout
, QSpacerItem
, and QWidgetItem
. So if you want to get the widget of a QLayoutItem
then you must use the widget() method:
def get_settings_value(self, items, value_type):
if value_type == 'Boolean':
layoutitem = next(items)
target_radio_button = layoutitem.widget()
print(type(target_radio_button))
#print (target_radio_button.isChecked())
Upvotes: 1