For a project in programming class we need to develop an application and have to use PYQT5 for the GUI.
I have a class based on QMainWindow. There are some lines of code I don't quite understand. Below is the initialization:
# Snippet 1
class ApplicationWindow(QtWidgets.QMainWindow):
def __init__(self):
QtWidgets.QMainWindow.__init__(self)
# self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
self.setWindowTitle("Main Window")
Then, the following variables/GUI elements are defined:
# Snippet 2
self.main_widget = QtWidgets.QWidget(self)
l = QtWidgets.QVBoxLayout(self.main_widget)
sc = MyStaticMplCanvas(width=5, height=4, dpi=100) # just some graph
dc = MyDynamicMplCanvas(width=5, height=4, dpi=100) # another graph
l.addWidget(sc)
l.addWidget(dc)
Then, I tried to add a horizontal box layout with the following content:
# Snippet 3
x = QtWidgets.QHBoxLayout(self.main_widget) # new
b1 = QtWidgets.QPushButton("Test1") # new
b2 = QtWidgets.QPushButton("Test2") # new
x.addWidget(p1) # new
x.addWidget(p2) # new
Lastly, the central widget is generated:
# Snippet 4
self.main_widget.setFocus()
self.setCentralWidget(self.main_widget)
The program itself produces no error and works properly. But only the two graphs in snippet 2 are displayed in the window. If you delete
self.main_widget
from
l = QtWidgets.QVBoxLayout(self.main_widget)
and leave the parenthesis empty, only the buttons in snippet 3 are displayed.
What is the meaning behind the following assignment?
self.main_widget = QtWidgets.QWidget(self)
Are you able to combine the vertical and horizontal box layout into a single one and therefore display the two buttons as well as the two graphs as the central widget? This was my original plan and I tried some things with the addLayout() and setLayout() options and wanted to add these layouts to the setCentralWidget() but was not successful.
Do you have an idea on how to display a combination of multliple box layouts as the central widget in a class based on QMainWindow?
Thank you very much. :)