toolBar.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. try:
  2. from PyQt5 import QtCore
  3. from PyQt5 import QtWidgets
  4. except ImportError:
  5. from PyQt4 import QtCore
  6. from PyQt4 import QtGui as QtWidgets
  7. class ToolBar(QtWidgets.QToolBar):
  8. def __init__(self, title):
  9. super(ToolBar, self).__init__(title)
  10. layout = self.layout()
  11. m = (0, 0, 0, 0)
  12. layout.setSpacing(0)
  13. layout.setContentsMargins(*m)
  14. self.setContentsMargins(*m)
  15. self.setWindowFlags(self.windowFlags() | QtCore.Qt.FramelessWindowHint)
  16. def addAction(self, action):
  17. if isinstance(action, QtWidgets.QWidgetAction):
  18. return super(ToolBar, self).addAction(action)
  19. btn = ToolButton()
  20. btn.setDefaultAction(action)
  21. btn.setToolButtonStyle(self.toolButtonStyle())
  22. self.addWidget(btn)
  23. class ToolButton(QtWidgets.QToolButton):
  24. """ToolBar companion class which ensures all buttons have the same size."""
  25. minSize = (60, 60)
  26. def minimumSizeHint(self):
  27. ms = super(ToolButton, self).minimumSizeHint()
  28. w1, h1 = ms.width(), ms.height()
  29. w2, h2 = self.minSize
  30. ToolButton.minSize = max(w1, w2), max(h1, h2)
  31. return QtCore.QSize(*ToolButton.minSize)