Chapter 3 - Create an empty QMainWindow¶
You can now think of presenting your data in a UI. A QMainWindow provides a convenient structure for GUI applications, such as a menu bar and status bar. The following image shows the layout that QMainWindow offers out-of-the box:
In this case, let your application inherit from QMainWindow, and add the following UI elements:
A “File” menu to open a File dialog.
An “Exit” menu close the window.
A status message on the status bar when the application starts.
In addition, you can define a fixed size for the window or adjust it based on the resolution you currently have. In the following snippet, you will see how window size is defined based on available screen width (80%) and height (70%).
You can achieve a similar structure using other Qt elements like QMenuBar, QWidget, and QStatusBar. Refer the QMainWindow layout for guidance.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
from PySide2.QtCore import Slot, qApp from PySide2.QtGui import QKeySequence from PySide2.QtWidgets import QMainWindow, QAction class MainWindow(QMainWindow): def __init__(self): QMainWindow.__init__(self) self.setWindowTitle("Eartquakes information") # Menu self.menu = self.menuBar() self.file_menu = self.menu.addMenu("File") # Exit QAction exit_action = QAction("Exit", self) exit_action.setShortcut(QKeySequence.Quit) exit_action.triggered.connect(self.close) self.file_menu.addAction(exit_action) # Status Bar self.status = self.statusBar() self.status.showMessage("Data loaded and plotted") # Window dimensions geometry = qApp.desktop().availableGeometry(self) self.setFixedSize(geometry.width() * 0.8, geometry.height() * 0.7)
Try running the script to see what output you get with it.
© 2020 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.