PyQt5 Make Multi Document Interface (MDI) Application – In this PyQt5 article we are
going to Make Multi Document Interface (MDI) Application.
so first of all lets have some basic information about Multi Document Interface (MDI) window.
Also you can check more Python GUI articles in the below links
1: Kivy GUI Development Tutorials
2: Python TKinter GUI Development
5: PyQt5 GUI Development Course
SDI & MDI Application
In many GUI applications, we will arrive at a situation where we need to open more
than one document at a time to process them. We will want to design our application
to handle this. This can be achieved by either of two approaches, namely, SDI and
MDI. A Single Document Interface (SDI) application implements this by creating
separate windows for each of the documents. This is done by creating a window
subclass that handles everything by itself, including loading, saving, cleaning up,
and so on. Each of the documents will be a clone of the main window having a
separate menu bar, toolbar, and status bar on its own. Each of the main window
instances must be able to act independently. However, there are some disadvantages
to this approach. This approach consumes a lot of resources and it would be very
inconvenient to open many windows at a time and keep track of them.
The second approach is to use a Multiple Document Interface (MDI) application
where the central widget is instantiated with multiple instances. All these widgets
will be interrelated within the main window and share the common menu bar,
toolbar, and other components. MDI applications will use lesser resources compared
with SDI applications. The MDI applications are provided with an extra menu to
manage between windows because shifting between them is not controlled by the
underlying operating system.
Implementation of MDI Application
A Multiple Document Interface application will consist of a main window where multiple
child windows and dialogs reside and appear for interaction. The central widget can be
PyQt5.QtWidget.QMdiArea widget. They are by themselves widget components that manage
the central area of the main window to arrange the MDI windows in a layout. Subwindows are
then created and added to the MDI area . An example of the MDI application is given as follows:
Complete code for PyQt5 Make Multi Document Interface (MDI) Application.
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 31 32 33 34 35 36 37 38 39 40 41 42 43 |
from PyQt5.QtWidgets import QApplication, QMainWindow, QMdiArea, QAction, QMdiSubWindow, QTextEdit import sys class MDIWindow(QMainWindow): count = 0 def __init__(self): super().__init__() self.mdi = QMdiArea() self.setCentralWidget(self.mdi) bar = self.menuBar() file = bar.addMenu("File") file.addAction("New") file.addAction("cascade") file.addAction("Tiled") file.triggered[QAction].connect(self.WindowTrig) self.setWindowTitle("MDI Application") def WindowTrig(self, p): if p.text() == "New": MDIWindow.count = MDIWindow.count + 1 sub = QMdiSubWindow() sub.setWidget(QTextEdit()) sub.setWindowTitle("Sub Window" + str(MDIWindow.count)) self.mdi.addSubWindow(sub) sub.show() if p.text() == "cascade": self.mdi.cascadeSubWindows() if p.text() == "Tiled": self.mdi.tileSubWindows() app = QApplication(sys.argv) mdi =MDIWindow() mdi.show() app.exec_() |
What is QMdiArea Class ?
QMdiArea functions, essentially, like a window manager for MDI windows. For instance,
it draws the windows it manages on itself and arranges them in a cascading or tile pattern. QMdiArea is commonly used as the center widget in a QMainWindow to create
MDI applications, but can also be placed in any layout. The following code adds an area to a
main window.
The MDI windows inside a main windowed application can be set in a cascade or tile
layout by default or a customized layout can be specified . The following screenshots
show the two types of layout of the example application:
This is the cascade mode
This is the tiled mode
Also you can watch the complete video for this article
Subscribe and Get Free Video Courses & Articles in your Email
You’re just amazing, keep it up, it’s helping a lot of people. I hope you have the recognition you deserve.
Thank you
Keep this going please, great job!