简体   繁体   English

如何将复选框与 PyQT5 中的按钮对齐?

[英]How to align a check box with a button in PyQT5?

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.mainLayout = QFormLayout()
        check_box = QCheckBox()
        my_button = QPushButton("My button")
        my_button.setFont(QFont("Roboto", 18))
        self.mainLayout.addRow(check_box, my_button)
        self.setLayout(self.mainLayout)


app = QApplication(sys.argv)
mainWindow = Window()
mainWindow.show()
sys.exit(app.exec_())

The check box is not in line with the button.复选框与按钮不对齐。 How can I line them up?我该如何排列它们? They both need to be a row on a QFormLayout.它们都需要在 QFormLayout 上排成一行。

When setting a widget as a "label" for QFormLayout, the alignment follows the default behavior, which is to align the widget on the top left (while taking into account the size policy).将小部件设置为 QFormLayout 的“标签”时,alignment 遵循默认行为,即在左上角对齐小部件(同时考虑大小策略)。

The labelAlignment property won't help here, as it only considers the horizontal alignment. labelAlignment属性在这里没有帮助,因为它只考虑水平alignment。

Trying to manually set the alignment of the layout item used for the "label" widget is not enough, as QFormLayout sets geometries programmatically, and "labels" are always top aligned.尝试手动设置用于“标签”小部件的布局项的 alignment 是不够的,因为 QFormLayout 以编程方式设置几何图形,并且“标签”始终是顶部对齐的。

To vertically center the widget, there are two possibilities:要将小部件垂直居中,有两种可能性:

Set an expanding vertical size policy设置扩展垂直尺寸政策

By default, QCheckBox has a Fixed vertical sizePolicy , meaning that it will always have the same height and therefore will be top aligned due to the default behavior of QFormLayout.默认情况下,QCheckBox 有一个Fixed vertical sizePolicy ,这意味着它始终具有相同的高度,因此由于 QFormLayout 的默认行为,它将是顶部对齐的。

You can change the vertical policy of the widget, and it will then be vertically centered:您可以更改小部件的垂直策略,然后它将垂直居中:

    check_box = QCheckBox()
    check_box.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Expanding)

Important : this will only work for widgets with controls that have a specific height, like QCheckBox or QRadioButton.重要提示:这仅适用于具有特定高度的控件的小部件,例如 QCheckBox 或 QRadioButton。 Setting an expanding vertical policy for "resizable" widgets (like QComboBox) will make that widget occupy the whole available height.为“可调整大小”的小部件(如 QComboBox)设置扩展垂直策略将使该小部件占据整个可用高度。 To avoid that, use the following solution.为避免这种情况,请使用以下解决方案。

Create a container创建一个容器

A more appropriate solution is to create a container for the actual widget, specify an expanding vertical size policy for that container, set a basic box layout and add the checkbox.更合适的解决方案是为实际的小部件创建一个容器,为该容器指定一个扩展的垂直大小策略,设置一个基本的框布局并添加复选框。

Basic layouts (QGridLayout, QBoxLayout) always try to make their widgets occupy all the available space in their layout "slot", and if the widget has a fixed size policy then it will be centered in the available rectangle.基本布局(QGridLayout、QBoxLayout)总是试图让它们的小部件占据其布局“槽”中的所有可用空间,如果小部件有固定大小策略,那么它将在可用矩形中居中。

    check_box = QCheckBox()
    check_container = QWidget()
    check_container.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Expanding)
    check_layout = QVBoxLayout(check_container)
    check_layout.setContentsMargins(0, 0, 0, 0) # Important!
    check_layout.addWidget(check_box)
    my_button = QPushButton("My button")
    my_button.setFont(QFont("Roboto", 18))
    self.mainLayout.addRow(check_container, my_button)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM