Course Content
Python PySide6
About Lesson

In the previous lesson we have learned that how we can create our first window with PySide6 In this lesson we want to talk about Python PySide6 Window Type Classes.

 

PySide6 provides different types of window classes for building different types of windows in desktop applications. these are some of key window type classes that are available in PySide6:

  1. QMainWindow: This is main window class in PySide6 and provides a standard main application window with a menu bar, toolbar, and status bar.
  2. QDialog: This class provides a dialog box that can be used to display messages or prompt the user for input.
  3. QMessageBox: This class provides a pre-built dialog box for displaying informative or error messages to the user.
  4. QFileDialog: This class provides a dialog box for selecting files or directories from the file system.
  5. QInputDialog: This class provides a dialog box for prompting the user to enter a value.
  6. QProgressDialog: This class provides a dialog box for displaying the progress of a long-running operation.
  7. QDockWidget: This class provides a widget that can be docked to the edges of a QMainWindow.
  8. QSplashScreen: This class provides a splash screen that can be displayed while the application is loading.
  9. QTabWidget: This class provides a widget that can display multiple tabs, each with its own set of content.

 

we can say that PySide6 provides rich set of window type classes that can be used to build  different types of desktop applications, from simple dialog boxes to complex main application windows. By leveraging these classes, developers can create applications that are both powerful and intuitive to use.

 

 

These are examples of using QDialog and QMainWindow in PySide6:

 

Example 1: Using QDialog

In this example, we create simple QDialog with QLabel and QPushButton, and setLayout() method is used to set QVBoxLayout as the layout for the dialog, and exec() method is used to display the dialog and wait for the user to interact with it.

 

 

Run the code and this will be the output.

Python PySide6 Window Type Classes
Python PySide6 Window Type Classes

 

 

Example 2: Using QMainWindow

In this example, we create QMainWindow with QLabel and QPushButton. also setWindowTitle() method is used to set the title for the window, and setCentralWidget() method is used to set the label as the central widget for the window. also addToolBar() method is used to add a toolbar to the window andaddWidget() method is used to add the button to the toolbar. alsoshow() method is used to display the window and exec() method is used to start the application event loop and wait for the user to interact with the window.

 

 

Run the code and this will be the result.

Python PySide6 Window Type Classes
Python PySide6 Window Type Classes

 

 

Difference between QDialog and QMainWindow

main difference between QDialog and QMainWindow is that QDialog is typically used to display modal or non-modal dialog boxes andQMainWindow is typically used as the main application window. QMainWindow provides more complete set of features for building main application windows, such as menu bar, toolbar, and status bar, while QDialog provides simpler set of features for building dialog boxes.

 

 

 

These are the examples of using QDialog and QMainWindow with object-oriented programming (OOP) in PySide6.

 

QDialog with OOP

In this example, we create MyDialog class that inherits from QDialog. and__init__ method is used to initialize the dialog by creating a QVBoxLayout, a QLabel and a QPushButton, setting the QVBoxLayout as the layout for the dialog, and displaying the dialog using self.exec().

 

 

QMainWindow with OOP

In this example, we create a MyWindow class that inherits from QMainWindow. The __init__ method is used to initialize the window by setting the window title, creating a QLabel and a QPushButton, setting the QLabel as the central widget for the window, and adding the button to a toolbar using self.addToolBar().addWidget(). Finally, the show() method is called to display the window.

By using OOP with PySide6, we can create reusable and maintainable code that is easy to read and understand. It also allows us to encapsulate the logic and behavior of the window or dialog within its own class, making it easier to manage and modify in the future.