The QMainWindow
class in PySide6 provides a main application window that includes standard features like a menu bar, toolbars, a status bar, and dock widgets. It serves as the foundation for creating complex GUI applications with a well-structured layout.
Key Features of QMainWindow
- Menu Bar: Provides a menu bar for adding menus and actions.
- Toolbars: Supports adding multiple toolbars for quick access to actions.
- Status Bar: Includes a status bar for displaying status information.
- Central Widget: Contains a central widget, which is the main content area of the window.
- Dock Widgets: Allows adding dockable widgets that can be moved and resized.
Creating a Basic QMainWindow
To create a QMainWindow
, subclass the QMainWindow
class and add components like a central widget, menu bar, toolbars, and status bar. Here's a basic example:
from PySide6.QtGui import QAction
from PySide6.QtWidgets import QApplication, QMainWindow, QTextEdit, QToolBar, QStatusBar
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('QMainWindow Example')
self.setMinimumSize(600, 400)
# Set the central widget
self.text_edit = QTextEdit()
self.setCentralWidget(self.text_edit)
# Create the menu bar
menu_bar = self.menuBar()
file_menu = menu_bar.addMenu("File")
# Add actions to the file menu
new_action = QAction("New", self)
open_action = QAction("Open", self)
save_action = QAction("Save", self)
exit_action = QAction("Exit", self)
exit_action.triggered.connect(self.close)
file_menu.addAction(new_action)
file_menu.addAction(open_action)
file_menu.addAction(save_action)
file_menu.addAction(exit_action)
# Create the toolbar
toolbar = QToolBar("Main Toolbar")
self.addToolBar(toolbar)
toolbar.addAction(new_action)
toolbar.addAction(open_action)
toolbar.addAction(save_action)
# Create the status bar
status_bar = QStatusBar()
self.setStatusBar(status_bar)
status_bar.showMessage("Ready")
app = QApplication([])
app.setStyle("Fusion")
window = MainWindow()
window.show()
app.exec()
In this example, a QMainWindow
is created with a central QTextEdit
widget, a menu bar with file operations, a toolbar with actions, and a status bar.
Adding a Dock Widget
QMainWindow
supports dock widgets, which can be moved around and docked in different areas of the window. Here's how to add a dock widget:
from PySide6.QtCore import Qt
from PySide6.QtGui import QAction
from PySide6.QtWidgets import QApplication, QMainWindow, QTextEdit, QToolBar, QStatusBar, QDockWidget, QListWidget
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('QMainWindow Example')
self.setMinimumSize(600, 400)
# Set the central widget
self.text_edit = QTextEdit()
self.setCentralWidget(self.text_edit)
# Create the menu bar
menu_bar = self.menuBar()
file_menu = menu_bar.addMenu("File")
# Add actions to the file menu
new_action = QAction("New", self)
open_action = QAction("Open", self)
save_action = QAction("Save", self)
exit_action = QAction("Exit", self)
exit_action.triggered.connect(self.close)
file_menu.addAction(new_action)
file_menu.addAction(open_action)
file_menu.addAction(save_action)
file_menu.addAction(exit_action)
# Create the toolbar
toolbar = QToolBar("Main Toolbar")
self.addToolBar(toolbar)
toolbar.addAction(new_action)
toolbar.addAction(open_action)
toolbar.addAction(save_action)
# Create the status bar
status_bar = QStatusBar()
self.setStatusBar(status_bar)
status_bar.showMessage("Ready")
# Dock widget
dock = QDockWidget("Dockable", self)
self.addDockWidget(Qt.DockWidgetArea.RightDockWidgetArea, dock)
list_widget = QListWidget()
list_widget.addItems(["Item 1", "Item 2", "Item 3"])
dock.setWidget(list_widget)
app = QApplication([])
app.setStyle("Fusion")
window = MainWindow()
window.show()
app.exec()
In this example, a dock widget containing a QListWidget
is added to the right side of the QMainWindow
.
Practical Example
Let's create a more practical example where QMainWindow
is used to build a simple text editor with a menu bar, toolbar, status bar, and dock widget:
from PySide6.QtCore import Qt
from PySide6.QtGui import QAction
from PySide6.QtWidgets import (
QApplication,
QMainWindow,
QTextEdit,
QToolBar,
QStatusBar,
QDockWidget,
QListWidget,
QFileDialog)
class TextEditor(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('Simple Text Editor')
self.setMinimumSize(800, 600)
self.text_edit = QTextEdit()
self.setCentralWidget(self.text_edit)
# Menu bar
menu_bar = self.menuBar()
file_menu = menu_bar.addMenu("File")
# Add actions to the file menu
new_action = QAction("New", self)
open_action = QAction("Open", self)
save_action = QAction("Save", self)
exit_action = QAction("Exit", self)
exit_action.triggered.connect(self.close)
new_action.triggered.connect(self.new_file)
open_action.triggered.connect(self.open_file)
save_action.triggered.connect(self.save_file)
file_menu.addAction(new_action)
file_menu.addAction(open_action)
file_menu.addAction(save_action)
file_menu.addAction(exit_action)
# Toolbar
toolbar = QToolBar("Main Toolbar")
self.addToolBar(toolbar)
toolbar.addAction(new_action)
toolbar.addAction(open_action)
toolbar.addAction(save_action)
# Status bar
status_bar = QStatusBar()
self.setStatusBar(status_bar)
status_bar.showMessage("Ready")
# Dock widget
dock = QDockWidget("Dockable", self)
self.addDockWidget(Qt.DockWidgetArea.RightDockWidgetArea, dock)
list_widget = QListWidget()
list_widget.addItems(["Item 1", "Item 2", "Item 3"])
dock.setWidget(list_widget)
def new_file(self):
self.text_edit.clear()
def open_file(self):
file_name, _ = QFileDialog.getOpenFileName(self, "Open File", "", "Text Files (*.txt);;All Files (*)")
if file_name:
with open(file_name, 'r') as file:
self.text_edit.setText(file.read())
def save_file(self):
file_name, _ = QFileDialog.getSaveFileName(self, "Save File", "", "Text Files (*.txt);;All Files (*)")
if file_name:
with open(file_name, 'w') as file:
file.write(self.text_edit.toPlainText())
app = QApplication([])
window = TextEditor()
window.show()
app.exec()
In this example, we create a simple text editor with basic file operations like new, open, and save. The QMainWindow
includes a menu bar, toolbar, status bar, and a dockable list widget.