QDialog 中的中心 OK 按钮
Center OK button in QDialog
是否可以在 QDialog 中将 OK 按钮居中?
class CustomDialog(QDialog):
def __init__(self, text, parent):
super().__init__(parent)
self.setFixedSize(200, 100)
self.setWindowFlags(Qt.WindowStaysOnTopHint | Qt.WindowTitleHint | Qt.Window
| Qt.CustomizeWindowHint)
QBtn = QDialogButtonBox.Ok
self.buttonBox = QDialogButtonBox(QBtn)
self.buttonBox.accepted.connect(self.accept)
self.buttonBox.rejected.connect(self.reject)
self.layout = QVBoxLayout()
message = QLabel(text)
self.layout.addWidget(message)
self.layout.addWidget(self.buttonBox)
self.setLayout(self.layout)
对话框显示仅确定按钮,我希望它居中而不是右侧
最简单的方法是使用 centerButtons
属性:
self.buttonBox.setCenterButtons(True)
还要考虑到默认情况下,小部件是通过尝试填充布局“单元格”中的所有可用 space 来添加到布局中的。指定对齐方式会导致使用其大小提示作为最大值来对齐小部件 space。
因此,另一种方法是更改为:
self.layout.addWidget(self.buttonBox, alignment=Qt.AlignCenter)
是否可以在 QDialog 中将 OK 按钮居中?
class CustomDialog(QDialog):
def __init__(self, text, parent):
super().__init__(parent)
self.setFixedSize(200, 100)
self.setWindowFlags(Qt.WindowStaysOnTopHint | Qt.WindowTitleHint | Qt.Window
| Qt.CustomizeWindowHint)
QBtn = QDialogButtonBox.Ok
self.buttonBox = QDialogButtonBox(QBtn)
self.buttonBox.accepted.connect(self.accept)
self.buttonBox.rejected.connect(self.reject)
self.layout = QVBoxLayout()
message = QLabel(text)
self.layout.addWidget(message)
self.layout.addWidget(self.buttonBox)
self.setLayout(self.layout)
对话框显示仅确定按钮,我希望它居中而不是右侧
最简单的方法是使用 centerButtons
属性:
self.buttonBox.setCenterButtons(True)
还要考虑到默认情况下,小部件是通过尝试填充布局“单元格”中的所有可用 space 来添加到布局中的。指定对齐方式会导致使用其大小提示作为最大值来对齐小部件 space。
因此,另一种方法是更改为:
self.layout.addWidget(self.buttonBox, alignment=Qt.AlignCenter)