Home > other >  Custom title bar with dockable toolbar
Custom title bar with dockable toolbar

Time:12-08

I want to create a custom title bar for my PyQt application, and my application uses dockable toolbars. To be dockable, the toolbars should be added to the MainWindow. However, with my custom toolbar being a Widget added to a frameless window, the toolbars dock themselves around the title bar. They can be docked above the title bar, and whenever docked on the sides, they push the title bar, which is not the expected behavior. I understand that this is due to the fact the the toolbar areas are always around the central widget of the window, and my custom title bar is inside the central widget. However, I don't see how I can make this work the way I want. Here is a MWE (I'm using PyQt=5.12.3) :

import sys
from typing import Optional

from PyQt5.QtCore import Qt, QSize, QPoint
from PyQt5.QtGui import QMouseEvent
from PyQt5.QtWidgets import QMainWindow, QApplication, QVBoxLayout, QPushButton, QWidget, QToolBar, QHBoxLayout, QLabel, \
    QToolButton


class CustomTitleBar(QWidget):
    def __init__(self, title: str, parent: Optional[QWidget] = None):
        super().__init__(parent=parent)
        self.window_parent = parent
        layout = QHBoxLayout()
        self.setObjectName("CustomTitleBar")
        self.setLayout(layout)
        layout.setContentsMargins(0, 0, 0, 0)
        layout.setSpacing(0)
        self.setFixedHeight(40)

        self.title = title
        self.title_label = QLabel(self.title)
        self.title_label.setObjectName("TitleBarLabel")
        layout.addWidget(self.title_label)
        layout.addStretch(1)

        but_minimize = QToolButton()
        but_minimize.setText("           
  • Related