Simply save the code below and import it into your project. You can add it to your main window by:

  1. QAutoHideDockWidgets(Qt.LeftDockWidgetArea, self)

and voila! your dockable widgets auto-hide.

  1. from PySide.QtCore import Qt, QEvent, QPointF
  2. from PySide.QtGui import *
  3.  
  4. class QAutoHideDockWidgets(QToolBar):
  5.     """
  6.    QMainWindow "mixin" which provides auto-hiding support for dock widgets
  7.    (not toolbars).
  8.    """
  9.     DOCK_AREA_TO_TB = {
  10.         Qt.LeftDockWidgetArea: Qt.LeftToolBarArea,
  11.         Qt.RightDockWidgetArea: Qt.RightToolBarArea,
  12.         Qt.TopDockWidgetArea: Qt.TopToolBarArea,
  13.         Qt.BottomDockWidgetArea: Qt.BottomToolBarArea,
  14.     }
  15.  
  16.     def __init__(self, area, parent, name="AUTO_HIDE"):
  17.         QToolBar.__init__(self, parent)
  18.         assert isinstance(parent, QMainWindow)
  19.         assert area in self.DOCK_AREA_TO_TB
  20.         self._area = area
  21.         self.setObjectName(name)
  22.         self.setWindowTitle(name)
  23.        
  24.         self.setFloatable(False)
  25.         self.setMovable(False)
  26.         w = QWidget(None)
  27.         w.resize(10, 100)
  28.         self.setSizePolicy(QSizePolicy(QSizePolicy.Fixed, QSizePolicy.MinimumExpanding))
  29.         self.addWidget(w)
  30.  
  31.         self.setAllowedAreas(self.DOCK_AREA_TO_TB[self._area])
  32.         self.parent().addToolBar(self.DOCK_AREA_TO_TB[self._area], self)
  33.         self.parent().centralWidget().installEventFilter(self)
  34.        
  35.         self.setVisible(False)
  36.         self.hideDockWidgets()
  37.  
  38.     def _dockWidgets(self):
  39.         mw = self.parent()
  40.         for w in mw.findChildren(QDockWidget, None):
  41.             if mw.dockWidgetArea(w) == self._area and not w.isFloating():
  42.                 yield w
  43.  
  44.     def paintEvent(self, event):
  45.         p = QPainter(self)
  46.         p.setPen(Qt.black)
  47.         p.setBrush(Qt.black)
  48.         if self._area == Qt.LeftDockWidgetArea:
  49.             p.translate(QPointF(0, self.height() / 2 - 5))
  50.             p.drawPolygon((QPointF(2,0), QPointF(8,5), QPointF(2,10)))
  51.         elif self._area == Qt.RightDockWidgetArea:
  52.             p.translate(QPointF(0, self.height() / 2 - 5))
  53.             p.drawPolygon((QPointF(8,0), QPointF(2,5), QPointF(8,10)))
  54.  
  55.     def _multiSetVisible(self, widgets, state):
  56.         if state:
  57.             self.setVisible(False)
  58.  
  59.         for w in widgets:
  60.             w.setUpdatesEnabled(False)
  61.         for w in widgets:
  62.             w.setVisible(state)
  63.         for w in widgets:
  64.             w.setUpdatesEnabled(True)
  65.  
  66.         if not state and widgets:
  67.             self.setVisible(True)
  68.  
  69.     def enterEvent(self, event):
  70.         self.showDockWidgets()
  71.  
  72.     def eventFilter(self, obj, event):
  73.         if event.type() == QEvent.Enter:
  74.             assert obj == self.parent().centralWidget()
  75.             self.hideDockWidgets()
  76.         return False
  77.  
  78.     def setDockWidgetsVisible(self, state):
  79.         self._multiSetVisible(list(self._dockWidgets()), state)
  80.  
  81.     def showDockWidgets(self): self.setDockWidgetsVisible(True)
  82.     def hideDockWidgets(self): self.setDockWidgetsVisible(False)