Collapsable Dock Windows

From Qt Wiki
Jump to: navigation, search
This article may require cleanup to meet the Qt Wiki's quality standards. Reason: Auto-imported from ExpressionEngine.
Please improve this article if you can. Remove the {{cleanup}} tag and add this page to Updated pages list after it's clean.

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

QAutoHideDockWidgets(Qt.LeftDockWidgetArea, self)<code>
 
and voila! your dockable widgets auto-hide.
from PySide.QtCore import Qt, QEvent, QPointF

from PySide.QtGui import *

class QAutoHideDockWidgets(QToolBar):

"""
QMainWindow "mixin" which provides auto-hiding support for dock widgets
(not toolbars).
"""
DOCK_AREA_TO_TB = {
Qt.LeftDockWidgetArea: Qt.LeftToolBarArea,
Qt.RightDockWidgetArea: Qt.RightToolBarArea,
Qt.TopDockWidgetArea: Qt.TopToolBarArea,
Qt.BottomDockWidgetArea: Qt.BottomToolBarArea,
}

def init(self, area, parent, name="AUTO_HIDE"):

QToolBar.init(self, parent)
assert isinstance(parent, QMainWindow)
assert area in self.DOCK_AREA_TO_TB
self._area = area
self.setObjectName(name)
self.setWindowTitle(name)

self.setFloatable(False)

self.setMovable(False)
w = QWidget(None)
w.resize(10, 100)
self.setSizePolicy(QSizePolicy(QSizePolicy.Fixed, QSizePolicy.MinimumExpanding))
self.addWidget(w)

self.setAllowedAreas(self.DOCK_AREA_TO_TB[self._area])

self.parent().addToolBar(self.DOCK_AREA_TO_TB[self._area], self)
self.parent().centralWidget().installEventFilter(self)

self.setVisible(False)

self.hideDockWidgets()

def _dockWidgets(self):

mw = self.parent()
for w in mw.findChildren(QDockWidget, None):
if mw.dockWidgetArea(w) == self._area and not w.isFloating():
yield w

def paintEvent(self, event):

p = QPainter(self)
p.setPen(Qt.black)
p.setBrush(Qt.black)
if self._area  Qt.LeftDockWidgetArea:
           p.translate(QPointF(0, self.height() / 2 - 5))
           p.drawPolygon((QPointF(2,0), QPointF(8,5), QPointF(2,10)))
       elif self._area  Qt.RightDockWidgetArea:
p.translate(QPointF(0, self.height() / 2 - 5))
p.drawPolygon((QPointF(8,0), QPointF(2,5), QPointF(8,10)))

def _multiSetVisible(self, widgets, state):

if state:
self.setVisible(False)

for w in widgets:

w.setUpdatesEnabled(False)
for w in widgets:
w.setVisible(state)
for w in widgets:
w.setUpdatesEnabled(True)

if not state and widgets:

self.setVisible(True)

def enterEvent(self, event):

self.showDockWidgets()

def eventFilter(self, obj, event):

if event.type()  QEvent.Enter:
           assert obj  self.parent().centralWidget()
self.hideDockWidgets()
return False

def setDockWidgetsVisible(self, state):

self._multiSetVisible(list(self._dockWidgets()), state)

def showDockWidgets(self): self.setDockWidgetsVisible(True)

def hideDockWidgets(self): self.setDockWidgetsVisible(False)