1

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?

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
rikoudosenin
  • 91
  • 2
  • 11
  • 1
    Qt has no control over the appearance of the title bar: it's all handled by the window manager. What do the title bars of non-Qt windows look like in your OS? They shouldn't look any different. If you want to change it, you will need to adjust your system settings accordingly. – ekhumoro May 27 '17 at 14:09
  • title bars of non-Qt windows are all left handed similar to the screenshot I provided, so basically there is no way to do it specifically for my app? does any other library gives control over this? if so which one(s) – rikoudosenin May 27 '17 at 14:51
  • Probably you would have to replace the normal title bar with a fake one. See [this answer](https://stackoverflow.com/q/9377914/984421). – ekhumoro May 27 '17 at 15:41

1 Answers1

1

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)...

yurisnm
  • 1,630
  • 13
  • 29