1 - What is the QMainWindow Class ?

The QMainWindow class has been specially designed to manage the main window of your application when it is complex. Among the features offered to us by the QMainWindow class, we find in particular the menus, the toolbar and the status bar. The QMainWindow class inherits directly from QWidget. It is a widget that is generally used once per program and is only used to create the main window of the application.

Note:

Simple pyqt applications won't need to use the QMainWindow class, they just use the QWidget class.

2 - General Structure Of The QMainWindow

A QMainWindow window consists of the following elements: 

  1. Menu Bar: this is the menu bar. This is where you will be able to create your File, Edit, View, Help, etc...
  2. Toolbars: the toolbars. In a text editor, for example, we have icons to create a new file, to save, etc... 
  3. Dock Widgets: more complex and more rarely used, these docks are containers that are placed around the main window. They can contain tools, for example the different types of brushes that one can use when making drawing software. 
  4. Central Widget: this is the heart of the window, where there will be the actual content. 
  5. Status Bar: this is the status bar. It usually displays the status of the program: Ready, Recording in progress etc...

3 - First QMainWindow

import sys
from PyQt5.QtWidgets import QWidget, QMainWindow, QApplication

class My_MainWindow(QWidget):

def setupUi(self, winObject):
winObject.setObjectName("My_Main_Window")
winObject.resize(500, 300)
winObject.setWindowTitle("This is my first MainWindow ! ")
winObject.setGeometry(100 , 100 , 500 , 300)

if __name__ == "__main__":

app = QApplication(sys.argv)
MainWindow = QMainWindow()
My_UI = My_MainWindow()
My_UI.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Younes Derfoufi
my-courses.net

One thought on “The QMainWindow Class”
  1. Hey what a brilliant post I have come across and believe me I have been searching out for this similar kind of post for past a week and hardly came across this. Thank you very much and will look for more postings from you Best hcia service provider.

Leave a Reply