I am pretty new to pyqt5.
I have a question regarding qstacklayout. It might be the same question as the How to make different pages of a QStackedWidget of different sizes? Let's assume that I have done this as below
class MainUI(QDialog)
def __init__(self, parent=None):
super().__init__(parent)
self.initUI()
def initUI(self):
self.scroll_area=QScrollArea()
self.main_layout=QHBoxLayout()
self.scroll_area.setWidgetResizable(True)
self.scrollAreaWidgetContents = QWidget()
self.scrollAreaWidgetContents.setGeometry(QRect(0, 0, 500, 300))
self.grid_layout = QGridLayout()
self.grid_layout.addWidget(self.a_widget_ui(), 0, 0)
... # Set other widget to grid_layout in position of (1, 0), (2, 0), (3, 0)
self.button_box = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
self.button_box.accepted.connect(self.accept)
self.button_box.rejected.connect(self.reject)
#set the buttonbox at the end of gridlayout
self.grid_layout.addWidget(self.button_box, 4, 0)
self.setLayout(self.main_layout)
self.setGeometry(300, 300, 790, 800)
def self.a_widget_ui(self):
group_box = QGroupBox('A Information')
a_list = ['1', '2', '3']
self.combo_box = QComboBox()
self.combo_box.addItems(a_list)
self.combo_box.activated[str].connect(self.update_widget)
self.stacked_layout = QStackedLayout()
self.A_layout = A_Layout()
self.B_layout = B_Layout()
self.stacked_layout.addWidget(self.A_Layout)
self.stacked_layout.addWidget(self.B_Layout)
hbox = QHBoxLayout()
hbox.addWidget(self.combo_box)
vbox = QVBoxLayout()
vbox.addLayout(hbox)
vbox.addLayout(self.stacked_layout)
group_box.setLayout(vbox)
return groupbox
def update_widget(self, choice):
if choice == "A":
self.stacked_layout.setCurrentWidget(self.A_Layout)
else:
self.stacked_layout.setCurrentWidget(self.B_Layout)
Then I have these different layouts for A_Layout() and B_Layout()
class A_Layout(QWidget):
def __init__(self, parent=None)
super().__init__(parent)
grid_layout = QGridLayout()
grid_layout.addWidget(self.set_a_widget(), 0, 0)
self.setLayout(grid_layout)
def set_a_widget(self):
main_widget = QWidget()
self._a_attrib = QLineEdit()
self._b_attrib = QLineEdit()
form_layout = QFormedLayout()
form_layout.addWidget('set a attrib', self._a_attrib)
form_layout.addWidget('set b attrib', self._b_attrib)
main_widget.setLayout(form_layout)
return main_widget
class B_Layout(QWidget):
def __init__(self, parent=None)
super().__init__(parent)
grid_layout = QGridLayout()
grid_layout.addWidget(self.set_a_widget(), 0, 0)
self.setLayout(grid_layout)
def set_a_widget(self):
main_widget = QWidget()
self._a_attrib = QLineEdit()
self._b_attrib = QLineEdit()
self._c_attrib = QLineEdit()
self._d_attrib = QLineEdit()
form_layout = QFormedLayout()
form_layout.addWidget('set a attrib', self._a_attrib)
form_layout.addWidget('set b attrib', self._b_attrib)
form_layout.addWidget('set c attrib', self._c_attrib)
form_layout.addWidget('set d attrib', self._d_attrib)
main_widget.setLayout(form_layout)
return main_widget
There are some conditions that I've made when I was making this structure.
- I made
class A_Layout
andclass B_Layout
in order for me to reuse those Dialog later if needed. - I used QStackedLayout / QStackedWidget in order for me to update the layout/widget via combobox.
My questions are the following.
- Is it possible for me to change the layout size dynamically? For example, as you can tell that the size of layout will be different between
A_Layout
andB_Layout
due to the number of components. - Since I am pretty new, but I have a hunch that I am doing it completely wrong about using qstackedwidget, maybe I should customize the layout such that it would work as qstackedlayout but has more size management
I would sincerely want to get advice or suggestions if it's possible. What I have tried is to put QSizePolicy before adding layout/widget in stackedlayout and put them whenever I call update_widget
. Any other tip or advice would be very appreciated.