Search code examples
pythonpyqt5centeringtitlebar

How to center window title in PyQt5?


I have set the window title with the following code:

w.setWindowTitle('PyQt5 Lesson 4')

with which I get:

Gui title Layout

Is there any way in pyqt5 to move the title, or simply center it?


Solution

  • I think the only way to do that would be to avoid using the default MenuBar your application have from your "SO". Set the attribute of your app to not use the default MenuBar and make your own. Try setting the attribute of your app and see if it works for you.

    app = QApplication(sys.argv)
    app.setAttribute(Qt.AA_DontUseNativeMenuBar)
    

    or only setting the windows flag of your main widget where your app is in.

    self.setWindowFlags(Qt.FramelessWindowHint)
    

    Something like that, but you still gotta develop your own "Fake Menu Bar" there you have total control about what you want to do with it.

    Here is a small example, looks kind of ugly(there are much more better practices to be used) but maybe it already can give you some ideas to what you really need:

    import sys
    
    from PyQt5.QtCore import QPoint
    from PyQt5.QtCore import Qt
    from PyQt5.QtWidgets import QApplication
    from PyQt5.QtWidgets import QHBoxLayout
    from PyQt5.QtWidgets import QLabel
    from PyQt5.QtWidgets import QVBoxLayout
    from PyQt5.QtWidgets import QWidget
    
    
    
    class MainWindow(QWidget):
    
        def __init__(self):
            super(MainWindow, self).__init__()
            self.layout  = QVBoxLayout()
            self.layout.addWidget(MyBar(self))
            self.layout.addStretch(-1)
            self.setLayout(self.layout)
            self.layout.setContentsMargins(0,0,0,0)
            self.layout.addStretch(-1)
            self.setFixedSize(800,400)
            self.setWindowFlags(Qt.FramelessWindowHint)
    
    
    class MyBar(QWidget):
    
        def __init__(self, parent):
            super(MyBar, self).__init__()
            self.parent = parent
            print(self.parent.width())
            self.layout = QHBoxLayout()
            self.layout.setContentsMargins(0,0,0,0)
            self.title = QLabel("My Own Bar")
            self.title.setFixedHeight(35)
            self.title.setAlignment(Qt.AlignCenter)
            self.layout.addWidget(self.title)
    
            self.title.setStyleSheet("""
                background-color: black;
                color: white;
            """)
            self.setLayout(self.layout)
    
            self.start = QPoint(0, 0)
            self.pressing = False
    
        def resizeEvent(self, QResizeEvent):
            super(MyBar, self).resizeEvent(QResizeEvent)
            self.title.setFixedWidth(self.parent.width())
    
        def mousePressEvent(self, event):
            self.start = self.mapToGlobal(event.pos())
            self.pressing = True
    
        def mouseMoveEvent(self, event):
            if self.pressing:
                self.end = self.mapToGlobal(event.pos())
                self.movement = self.end-self.start
                self.parent.move(self.mapToGlobal(self.movement))
                self.start = self.end
    
        def mouseReleaseEvent(self, QMouseEvent):
            self.pressing = False
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        mw = MainWindow()
        mw.show()
        sys.exit(app.exec_())
    

    Note: Observe that since it's now Frameless it kind of "lose" its property of moving around so I had to re-implement it, the same would apply to resize and any other property that would need it frame(e.g. close, mini and max buttons)...