tool_bar.py 1.1 KB

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