PyQt5 QDockWidget - Getting Allowed Areas

PyQt5 QDockWidget - Getting Allowed Areas

In PyQt5, a QDockWidget is a widget that can be docked inside a QMainWindow or floated as a top-level window. The areas where a QDockWidget can be docked in a QMainWindow are determined by the dock widget's allowed areas.

To get the allowed areas of a QDockWidget, you can use the allowedAreas() method. This method returns an OR combination of the Qt.DockWidgetArea values that specify where the dock widget can be docked.

Here's how you can use the allowedAreas() method to get the allowed areas of a QDockWidget:

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QDockWidget, QTextEdit
from PyQt5.QtCore import Qt

class Example(QMainWindow):
    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):
        self.setGeometry(100, 100, 800, 600)

        # Create a QDockWidget
        dock = QDockWidget("Dockable", self)
        te = QTextEdit(self)
        dock.setWidget(te)

        # Set allowed areas
        dock.setAllowedAreas(Qt.LeftDockWidgetArea | Qt.RightDockWidgetArea)

        self.addDockWidget(Qt.LeftDockWidgetArea, dock)

        # Get allowed areas of the QDockWidget
        areas = dock.allowedAreas()

        if areas & Qt.LeftDockWidgetArea:
            print("Docking on the left side is allowed.")
        if areas & Qt.RightDockWidgetArea:
            print("Docking on the right side is allowed.")
        if areas & Qt.TopDockWidgetArea:
            print("Docking on the top side is allowed.")
        if areas & Qt.BottomDockWidgetArea:
            print("Docking on the bottom side is allowed.")

        self.show()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

In this example, the QDockWidget is allowed to dock on the left and right sides of the QMainWindow. The allowedAreas() method is then used to retrieve these areas and print out where docking is allowed.


More Tags

var persist typescript1.8 visual-studio-2012 refactoring vhosts react-test-renderer datagrid gmail-imap pod-install

More Programming Guides

Other Guides

More Programming Examples