Search code examples
pythonqtpysideqmainwindowqdockwidget

Collect all docked widgets and their locations


Is there a way in PySide to collect all the docked widgets, and whether they are docked on he left or right side of the main window?

If there is no answer to my first question, how can I at least find out if a widget is docked on the left or the right?

from PySide import QtCore, QtGui


class MainWindow(QtGui.QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()

        self.textEdit = QtGui.QTextEdit()
        self.setCentralWidget(self.textEdit)

        self.createActions()
        self.createMenus()
        self.createToolBars()
        self.createStatusBar()
        self.createDockWindows()

        self.setWindowTitle("Dock Widgets")

    def createActions(self):
        self.toggleLeftAct = QtGui.QAction(
                "&Toggle Left", self, shortcut=QtGui.QKeySequence("Ctrl+L"),
                statusTip="Toggle Left Sidebar",
                triggered=self.toggleLeft)

        self.toggleRightAct = QtGui.QAction(
                "&Toggle Right", self, shortcut=QtGui.QKeySequence("Ctrl+R"),
                statusTip="Toggle Right Sidebar",
                triggered=self.toggleRight)

    def createMenus(self):
        self.fileMenu = self.menuBar().addMenu("&File")
        self.fileMenu.addAction(self.toggleLeftAct)
        self.fileMenu.addAction(self.toggleRightAct)

        self.viewMenu = self.menuBar().addMenu("&View")

    def createToolBars(self):
        self.fileToolBar = self.addToolBar("File")
        self.fileToolBar.addAction(self.toggleLeftAct)
        self.fileToolBar.addAction(self.toggleRightAct)

    def createStatusBar(self):
        self.statusBar().showMessage("Ready")

    def createDockWindows(self):
        self.dockCustomers = QtGui.QDockWidget("Customers", self)
        self.dockCustomers.setAllowedAreas(QtCore.Qt.LeftDockWidgetArea | QtCore.Qt.RightDockWidgetArea)
        self.customerList = QtGui.QListWidget(self.dockCustomers)
        self.dockCustomers.setWidget(self.customerList)
        self.addDockWidget(QtCore.Qt.RightDockWidgetArea, self.dockCustomers)
        self.viewMenu.addAction(self.dockCustomers.toggleViewAction())

        self.dockParagraphs = QtGui.QDockWidget("Paragraphs", self)
        self.paragraphsList = QtGui.QListWidget(self.dockParagraphs)
        self.dockParagraphs.setWidget(self.paragraphsList)
        self.addDockWidget(QtCore.Qt.RightDockWidgetArea, self.dockParagraphs)
        self.viewMenu.addAction(self.dockParagraphs.toggleViewAction())

    def toggleLeft(self):
        print self.dockCustomers
        print self.dockParagraphs

    def toggleRight(self):
        print 'toggle Right'

if __name__ == '__main__':

    import sys

    app = QtGui.QApplication(sys.argv)
    mainWin = MainWindow()
    mainWin.show()
    sys.exit(app.exec_())

Solution

  • There is no convenience method for getting the list of dock-widgets. However, whenever a dock-widget is added it will be automatically reparented to the main-window, so you can use the findChildren method of the main window instead:

    for dock in self.findChildren(QtGui.QDockWidget):
        print(dock.windowTitle())
    

    The area where a dock-widget is currently located can be found with the dockWidgetArea method of the main window:

        area = self.dockWidgetArea(dock)
        if area == QtCore.Qt.LeftDockWidgetArea:
            print dock.windowTitle(), '(Left)'
        elif area == QtCore.Qt.RightDockWidgetArea:
            print dock.windowTitle(), '(Right)'