In this example
Stack = QStackedWidget (self)
stack1 = QWidgetA()
stack2 = QWidgetB()
stack3 = QWidgetC()
Stack.addWidget (self.stack1)
Stack.addWidget (self.stack2)
Stack.addWidget (self.stack3)
is there any way to iterate over Stack to retrieve the widgets that are the various items in the Stack?
I haven't found a function in QStackedWidget
Ideally I would be able to get back the instances of QWidgetA, QwidgetB, and QwidgetC that are in stack1, stack2, and stack3.
Get the QStackedWidget::count()
of items and iterate from zero to < count()
using your favorite loop flavor. Each QWidget you added can then be gotten with QStackedWidget::widget(index)
@MaximPaperno' is the solution in c++ speak. I gave it the checkmark. Here is the Python/PyQt code that demonstrates his answer.
from PyQt5 import QtWidgets
class QWidgetA(QtWidgets.QWidget):
def p(self):
print('A')
class QWidgetB(QtWidgets.QWidget):
def p(self):
print('B')
class QWidgetC(QtWidgets.QWidget):
def p(self):
print('C')
if __name__ == '__main__':
app = QtWidgets.QApplication([])
Stack = QtWidgets.QStackedWidget()
stack1 = QWidgetA()
stack2 = QWidgetB()
stack3 = QWidgetC()
Stack.addWidget(stack1)
Stack.addWidget(stack2)
Stack.addWidget(stack3)
x = Stack.count() #<<<<
for i in range(x): #<<<<
w = Stack.widget(i) #<<<<
w.p()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.