PyQt5 QDockWidget and its Features
PyQt5 is a python binding of the C++ application framework called Qt. Qt is an open-source cross-platform framework that helps in developing professional applications with great user interfaces.
QDockWidget is one of the many classes in Qt that provides a dockable window feature. It enables the users to dock and undock widgets within the main window of the application. The main feature of QDockWidget is to provide a customizable and easily accessible interface for the users.
In this article, we’ll discuss the basics of QDockWidget and its many features with the help of an example.
Setting Up PyQt5 Environment
Before we proceed, you need to have PyQt5 installed on your system. If not, you can install it by running the following command in your terminal:
pip install pyqt5
Creating a Dockable Window
To create a QDockWidget, we simply instantiate the class and add our widget to it as shown below:
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QDockWidget, QTextEdit
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.initGUI()
def initGUI(self):
self.setWindowTitle('QDockWidget Demo')
# Create a central widget
centralWidget = QTextEdit()
self.setCentralWidget(centralWidget)
# Create a dock widget
dockWidget = QDockWidget("Dockable Window", self)
# Make the widget a central widget
dockWidget.setWidget(QTextEdit())
# Add dock widget to the left side
self.addDockWidget(1, dockWidget)
# Create a QApplication instance
app = QApplication(sys.argv)
# Create a QMainWindow instance
window = MainWindow()
window.show()
# Run the event loop
sys.exit(app.exec_())
We first create a QMainWindow
instance and set it to self
. A centralWidget
widget is added to the main window using the setCentralWidget()
method. We then create a QDockWidget
instance and set its name to Dockable Window
. We then set a QTextEdit()
widget to the dock widget through the setWidget()
method. Lastly, we add the QDockWidget
instance to the left side of the main window using the addDockWidget()
method.
When this code is executed, you will have a basic application that has a central widget and a dock widget to the left.
Adding Features to QDockWidget
QDockWidget has many features that can be customized to meet your needs. Let’s see some of the most important ones.
Setting a Dock Widget
You can simply set and unset a dock widget by calling the setFloating()
method. If the setFloating()
is set to True
, it will make it a floating widget, and if it is False
, it will make it a dockable widget.
# Set the dock widget to be a floating widget
dockWidget.setFloating(True)
# Set the dock widget to be a dockable widget
dockWidget.setFloating(False)
Setting Dock Widget Integration
By default, dock widgets are integrated within the main window’s layout. But sometimes, we need to make it a separate widget that can be opened from the taskbar.
# Set the dock widget to be separate from the main window
dockWidget.setFeatures(QDockWidget.DockWidgetFloatable | QDockWidget.DockWidgetMovable)
dockWidget.setAllowedAreas(QtCore.Qt.NoDockWidgetArea)
Here, we add DockWidgetFloatable
to make it floatable and DockWidgetMovable
to indicate that the widget is movable by the user. We then set the QDockWidget
instance to NoDockWidgetArea
meaning it will not have access to any docking area, instead of creating it as a separate window.
Setting The Dock Widget Title
Setting the title of the dock widget is as simple as setting its name.
dockWidget.setWindowTitle('My Dock Widget')
Showing and Hiding Dock Widgets
We can also show or hide the dock widget entirely by using the setVisible()
method.
# Show the dock widget
dockWidget.setVisible(True)
# Hide the dock widget
dockWidget.setVisible(False)
Conclusion
QDockWidget is a useful and powerful widget to use in your PyQt5 applications. It provides ease of access to the user and flexibility to your application. Hopefully, this article has given you insight into the various features of QDockWidget and helps you to integrate it into your projects.