我已使用以下代码设置窗口标题:
w.setWindowTitle('PyQt5 Lesson 4')
我得到了:
pyqt5中有没有办法移动标题,或者只是居中?
答案 0 :(得分:1)
我认为唯一可行的方法是避免使用您的应用程序从您的" SO"中使用的默认MenuBar。将应用的属性设置为不使用默认的MenuBar并自行创建。 尝试设置应用的属性,看看它是否适合您。
app = QApplication(sys.argv)
app.setAttribute(Qt.AA_DontUseNativeMenuBar)
或仅设置应用所在的主窗口小部件的Windows标志。
self.setWindowFlags(Qt.FramelessWindowHint)
类似的东西,但你仍然需要开发自己的假冒菜单栏"在那里你可以完全控制你想用它做什么。
这是一个小例子,看起来很丑陋(有更多更好的做法可以使用)但也许它已经可以给你一些你真正需要的想法:
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_())
注意: 观察一下,因为它现在是无帧的,它会失去"失去"它的移动属性,所以我不得不重新实现它,同样适用于调整大小和任何其他需要它的属性框架(例如,关闭,迷你和最大按钮)......