pyqt5 将QGroupBox('My Group Box')中的’My Group Box‘居中显示
时间: 2024-01-03 16:04:58 浏览: 150
要将QGroupBox()中的标题居中显示,可以通过设置QGroupBox()的样式表来实现。代码如下:
```python
from PyQt5.QtWidgets import QApplication, QGroupBox, QLabel, QVBoxLayout, QWidget
from PyQt5.QtCore import Qt
app = QApplication([])
window = QWidget()
groupbox = QGroupBox('My Group Box')
groupbox.setStyleSheet('QGroupBox::title {subcontrol-origin: margin; subcontrol-position: top center;}')
label = QLabel('Hello World')
layout = QVBoxLayout()
layout.addWidget(label)
groupbox.setLayout(layout)
main_layout = QVBoxLayout()
main_layout.addWidget(groupbox)
window.setLayout(main_layout)
window.show()
app.exec_()
```
在这个例子中,我们使用了QGroupBox()的样式表来设置标题的样式。具体来说,我们设置了QGroupBox::title的subcontrol-origin为margin,表示标题的起点为边缘,subcontrol-position为top center,表示标题在顶部中央。最后,我们将QGroupBox()添加到QWidget()中,并显示窗口。运行程序后,你会看到QGroupBox()的标题已经居中显示了。
阅读全文
相关推荐
















