Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom TitleBar functionality #15

Open
PanagiotisMenounos opened this issue Sep 16, 2024 · 0 comments
Open

Custom TitleBar functionality #15

PanagiotisMenounos opened this issue Sep 16, 2024 · 0 comments

Comments

@PanagiotisMenounos
Copy link

Seems like it is not straightforward to create a CustomTitleBar like this example where the buttons are always in the middle of the title bar:

2isXhHbOQP.mp4

I did this by overriding the class in: venv\Lib\site-packages\pyqt_frameless_window\windows\titleBar.py

class TitleBar(QWidget):
    def __init__(self, base_widget=None, hint=None):
        super().__init__(base_widget)
        self.__initVal(hint)
        self.__initUi()

    def __initVal(self, hint):
        self._pressToMove = True
        self.__baseWindowResizable = True

        # Variables for icon and title
        self.__icon = QIcon()
        self.__iconLbl = QLabel()
        self.__titleLbl = QLabel()

        # Corner widget
        self.__cornerWidget = QWidget()

        # Control buttons
        self.__fullScreenBtn = QPushButton('▣')
        self.__minBtn = QPushButton('🗕')
        self.__maxBtn = QPushButton('🗖')
        self.__closeBtn = QPushButton('🗙')

        # Custom buttons
        self.customButton1 = QPushButton("Button1")
        self.customButton2 = QPushButton("Button2")
        self.customButton3 = QPushButton("Button3")

        # Connect signals
        self.__fullScreenBtn.setCheckable(True)
        self.__fullScreenBtn.clicked.connect(self.__fullScreen)
        self.__minBtn.clicked.connect(self.window().showMinimized)
        self.__maxBtn.clicked.connect(self.__maximize)
        self.__closeBtn.clicked.connect(self.window().close)

        # self._customButton1.clicked.connect(self.on_custom_button1_clicked)
        # self._customButton2.clicked.connect(self.on_custom_button2_clicked)

        self.__btn_dict = {
            'full_screen': self.__fullScreenBtn,
            'min': self.__minBtn,
            'max': self.__maxBtn,
            'close': self.__closeBtn
        }

        self.__hint = hint

    def __initUi(self):
        lay = QHBoxLayout()
        lay.setContentsMargins(0, 0, 0, 0)
        lay.setSpacing(0)
        lay.setAlignment(Qt.AlignLeft)

        lay.addWidget(self.__iconLbl)
        lay.addWidget(self.__titleLbl)

        # Add stretch to push custom buttons to the center
        lay.addStretch(1)

        # Add custom buttons
        lay.addWidget(self.customButton1)
        lay.addWidget(self.customButton2)
        lay.addWidget(self.customButton3)

        # Add stretch to push control buttons to the right
        lay.addStretch(1)

        # Add corner widget containing control buttons
        self.__setCornerWidgetLayout()
        self.setTitleBarHint(self.__hint)
        lay.addWidget(self.__cornerWidget)

        self._styleInit()

        self.window().installEventFilter(self)

        self.setLayout(lay)
  • It would be better to create a class CustomTitleBar in the main.py and use it without need to override the base class TitleBar(QWidget):
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant