qt qmenu字体居中
时间: 2023-09-25 10:16:11 浏览: 78
可以通过设置QMenu的StyleSheet来实现QMenu中字体居中。
以下是一个简单的例子:
```python
from PyQt5.QtWidgets import QApplication, QMainWindow, QMenu, QAction
class Example(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
menubar = self.menuBar()
fileMenu = menubar.addMenu('File')
# 添加菜单项
fileMenu.addAction('New')
fileMenu.addAction('Open')
fileMenu.addAction('Save')
# 设置菜单项字体居中
fileMenu.setStyleSheet("QMenu::item { padding: 2px 20px 2px 20px; }")
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('QMenu Example')
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
```
在上面的例子中,我们通过设置`QMenu::item`样式来设置菜单项的内边距,实现了菜单项中字体居中。具体来说,`padding: 2px 20px 2px 20px;`表示上下左右的内边距都为2px,左右内边距为20px。你可以根据需要进行调整。