我想设置复选框边距?结盟
I wanna set check box margin ? alignment
我做了一些 Qdialog
,当我执行代码时,复选框没有均匀对齐 :
但我想这样:
代码如下:
class SelectPay_Dialog2(Qdialog):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(500, 200, 400, 200)
self.setWindowTitle("Window")
label1 = QLabel("test : ")
label2 = QLabel("test : ")
label3 = QLabel("test : ")
#
label1.setFixedSize(80, 20)
label2.setFixedSize(80, 20)
label3.setFixedSize(80, 20)
cb1 = QCheckBox('1week')
cb2 = QCheckBox('2week')
cb3 = QCheckBox('3week')
cb4 = QCheckBox('4week')
cb5 = QCheckBox('5week')
cb6 = QCheckBox('6week')
self.input_date_text1 = QLineEdit()
self.input_date_text2 = QLineEdit()
self.input_date_text1.setFixedSize(100, 25)
self.input_date_text2.setFixedSize(100, 25)
self.input_date_text1.setReadOnly(True)
self.input_date_text2.setReadOnly(True)
layout = QGridLayout()
layout.addWidget(label1, 0, 0)
layout.addWidget(label2, 2, 0)
layout.addWidget(label3, 1, 0)
layout.addWidget(self.input_date_text1, 2, 1)
layout.addWidget(self.input_date_text2, 2, 2)
layout.addWidget(cb1, 1, 1 )
layout.addWidget(cb2, 1, 2 )
layout.addWidget(cb3, 1, 3 )
layout.addWidget(cb4, 1, 4)
layout.addWidget(cb5, 1, 5)
self.setLayout(layout)
你写的地方layout.addWidget(cb1, 1, 1 )
你的括号间距与layout.addWidget(cb4, 1, 4)
不同layout.addWidget(cb4, 1, 4)
使它们都相等并且它应该修复它
顾名思义,QGridLayout 将项目放置在具有行和列的 网格 中。如果您更仔细地查看图像,您会发现前两个复选框与行编辑对齐。
如果您不想要这种行为,那么您有不同的选择,所有这些都使用 QHBoxLayout:
- 完全不使用网格布局,而是使用QVBoxLayout作为主要布局,并为每一“行”添加QHBoxLayout;这有一个缺点,即第一个“列”可能未对齐,这似乎是您的情况的要求;
- 使用QFormLayout,
addRow
右边是QHBoxLayout;
- 添加具有正确列跨度的 QHBoxLayout;
第三种情况可能对您通常在右“边”有固定数量的小部件的情况有用。
考虑到将两行编辑视为“默认”布局安排,这是如何寻找您的情况:
checkLayout = QHBoxLayout()
layout.addLayout(checkLayout, 1, 1, 1, 3)
checkLayout.addWidget(cb1)
checkLayout.addWidget(cb2)
checkLayout.addWidget(cb3)
checkLayout.addWidget(cb4)
checkLayout.addWidget(cb5)
请注意,我使用了 3 的列跨度。这确保布局占用 3 列,并且由于实际只占用两列(由行编辑),结果是剩余的 space 将仅用于所需的 remaining 复选框的宽度。
一个类似的相关解决方案需要将行编辑添加到他们自己的 QHBoxLayout 中,而且还需要在 addStretch()
之后使用它们,以便在他们的权利。
对于复杂的布局,我建议使用 Qt 设计器。您可以使用 pyuic5
工具将使用它创建的 ui
文件翻译成 python 代码。
main.py
from PyQt5 import QtWidgets, QtCore
from Ui_SelectPay_Dialog2 import Ui_SelectPay_Dialog2
class SelectPay_Dialog2(QtWidgets.QDialog):
def __init__(self, parent = None):
super().__init__(parent)
ui = Ui_SelectPay_Dialog2()
ui.setupUi(self)
if __name__ == "__main__":
app = QtWidgets.QApplication([])
widget = SelectPay_Dialog2()
widget.show()
app.exec_()
Ui_SelectPay_Dialog2.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_SelectPay_Dialog2(object):
def setupUi(self, SelectPay_Dialog2):
SelectPay_Dialog2.setObjectName("SelectPay_Dialog2")
SelectPay_Dialog2.resize(432, 150)
self.gridLayout = QtWidgets.QGridLayout(SelectPay_Dialog2)
self.gridLayout.setObjectName("gridLayout")
self.label = QtWidgets.QLabel(SelectPay_Dialog2)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.label.sizePolicy().hasHeightForWidth())
self.label.setSizePolicy(sizePolicy)
self.label.setObjectName("label")
self.gridLayout.addWidget(self.label, 0, 0, 1, 1)
self.label_2 = QtWidgets.QLabel(SelectPay_Dialog2)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.label_2.sizePolicy().hasHeightForWidth())
self.label_2.setSizePolicy(sizePolicy)
self.label_2.setObjectName("label_2")
self.gridLayout.addWidget(self.label_2, 1, 0, 1, 1)
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.cb1 = QtWidgets.QCheckBox(SelectPay_Dialog2)
self.cb1.setObjectName("cb1")
self.horizontalLayout.addWidget(self.cb1)
self.cb2 = QtWidgets.QCheckBox(SelectPay_Dialog2)
self.cb2.setObjectName("cb2")
self.horizontalLayout.addWidget(self.cb2)
self.cb3 = QtWidgets.QCheckBox(SelectPay_Dialog2)
self.cb3.setObjectName("cb3")
self.horizontalLayout.addWidget(self.cb3)
self.cb4 = QtWidgets.QCheckBox(SelectPay_Dialog2)
self.cb4.setObjectName("cb4")
self.horizontalLayout.addWidget(self.cb4)
self.cb5 = QtWidgets.QCheckBox(SelectPay_Dialog2)
self.cb5.setObjectName("cb5")
self.horizontalLayout.addWidget(self.cb5)
self.cb6 = QtWidgets.QCheckBox(SelectPay_Dialog2)
self.cb6.setObjectName("cb6")
self.horizontalLayout.addWidget(self.cb6)
self.gridLayout.addLayout(self.horizontalLayout, 1, 1, 1, 1)
self.label_3 = QtWidgets.QLabel(SelectPay_Dialog2)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.label_3.sizePolicy().hasHeightForWidth())
self.label_3.setSizePolicy(sizePolicy)
self.label_3.setObjectName("label_3")
self.gridLayout.addWidget(self.label_3, 2, 0, 1, 1)
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.input_date_text1 = QtWidgets.QLineEdit(SelectPay_Dialog2)
self.input_date_text1.setObjectName("input_date_text1")
self.horizontalLayout_2.addWidget(self.input_date_text1)
self.input_date_text2 = QtWidgets.QLineEdit(SelectPay_Dialog2)
self.input_date_text2.setObjectName("input_date_text2")
self.horizontalLayout_2.addWidget(self.input_date_text2)
spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_2.addItem(spacerItem)
self.gridLayout.addLayout(self.horizontalLayout_2, 2, 1, 1, 1)
self.gridLayout.setRowStretch(0, 1)
self.gridLayout.setRowStretch(1, 1)
self.gridLayout.setRowStretch(2, 1)
self.retranslateUi(SelectPay_Dialog2)
QtCore.QMetaObject.connectSlotsByName(SelectPay_Dialog2)
def retranslateUi(self, SelectPay_Dialog2):
_translate = QtCore.QCoreApplication.translate
SelectPay_Dialog2.setWindowTitle(_translate("SelectPay_Dialog2", "Form"))
self.label.setText(_translate("SelectPay_Dialog2", "test : "))
self.label_2.setText(_translate("SelectPay_Dialog2", "test : "))
self.cb1.setText(_translate("SelectPay_Dialog2", "1week"))
self.cb2.setText(_translate("SelectPay_Dialog2", "2week"))
self.cb3.setText(_translate("SelectPay_Dialog2", "3week"))
self.cb4.setText(_translate("SelectPay_Dialog2", "4week"))
self.cb5.setText(_translate("SelectPay_Dialog2", "5week"))
self.cb6.setText(_translate("SelectPay_Dialog2", "6week"))
self.label_3.setText(_translate("SelectPay_Dialog2", "test : "))
dialog2.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>SelectPay_Dialog2</class>
<widget class="QDialog" name="SelectPay_Dialog2">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>432</width>
<height>150</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QGridLayout" name="gridLayout" rowstretch="1,1,1">
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>test : </string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_2">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>test : </string>
</property>
</widget>
</item>
<item row="1" column="1">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QCheckBox" name="cb1">
<property name="text">
<string>1week</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="cb2">
<property name="text">
<string>2week</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="cb3">
<property name="text">
<string>3week</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="cb4">
<property name="text">
<string>4week</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="cb5">
<property name="text">
<string>5week</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="cb6">
<property name="text">
<string>6week</string>
</property>
</widget>
</item>
</layout>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_3">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>test : </string>
</property>
</widget>
</item>
<item row="2" column="1">
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLineEdit" name="input_date_text1"/>
</item>
<item>
<widget class="QLineEdit" name="input_date_text2"/>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
我做了一些 Qdialog
,当我执行代码时,复选框没有均匀对齐 :
但我想这样:
代码如下:
class SelectPay_Dialog2(Qdialog):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(500, 200, 400, 200)
self.setWindowTitle("Window")
label1 = QLabel("test : ")
label2 = QLabel("test : ")
label3 = QLabel("test : ")
#
label1.setFixedSize(80, 20)
label2.setFixedSize(80, 20)
label3.setFixedSize(80, 20)
cb1 = QCheckBox('1week')
cb2 = QCheckBox('2week')
cb3 = QCheckBox('3week')
cb4 = QCheckBox('4week')
cb5 = QCheckBox('5week')
cb6 = QCheckBox('6week')
self.input_date_text1 = QLineEdit()
self.input_date_text2 = QLineEdit()
self.input_date_text1.setFixedSize(100, 25)
self.input_date_text2.setFixedSize(100, 25)
self.input_date_text1.setReadOnly(True)
self.input_date_text2.setReadOnly(True)
layout = QGridLayout()
layout.addWidget(label1, 0, 0)
layout.addWidget(label2, 2, 0)
layout.addWidget(label3, 1, 0)
layout.addWidget(self.input_date_text1, 2, 1)
layout.addWidget(self.input_date_text2, 2, 2)
layout.addWidget(cb1, 1, 1 )
layout.addWidget(cb2, 1, 2 )
layout.addWidget(cb3, 1, 3 )
layout.addWidget(cb4, 1, 4)
layout.addWidget(cb5, 1, 5)
self.setLayout(layout)
你写的地方layout.addWidget(cb1, 1, 1 )
你的括号间距与layout.addWidget(cb4, 1, 4)
不同layout.addWidget(cb4, 1, 4)
使它们都相等并且它应该修复它
顾名思义,QGridLayout 将项目放置在具有行和列的 网格 中。如果您更仔细地查看图像,您会发现前两个复选框与行编辑对齐。
如果您不想要这种行为,那么您有不同的选择,所有这些都使用 QHBoxLayout:
- 完全不使用网格布局,而是使用QVBoxLayout作为主要布局,并为每一“行”添加QHBoxLayout;这有一个缺点,即第一个“列”可能未对齐,这似乎是您的情况的要求;
- 使用QFormLayout,
addRow
右边是QHBoxLayout; - 添加具有正确列跨度的 QHBoxLayout;
第三种情况可能对您通常在右“边”有固定数量的小部件的情况有用。
考虑到将两行编辑视为“默认”布局安排,这是如何寻找您的情况:
checkLayout = QHBoxLayout()
layout.addLayout(checkLayout, 1, 1, 1, 3)
checkLayout.addWidget(cb1)
checkLayout.addWidget(cb2)
checkLayout.addWidget(cb3)
checkLayout.addWidget(cb4)
checkLayout.addWidget(cb5)
请注意,我使用了 3 的列跨度。这确保布局占用 3 列,并且由于实际只占用两列(由行编辑),结果是剩余的 space 将仅用于所需的 remaining 复选框的宽度。
一个类似的相关解决方案需要将行编辑添加到他们自己的 QHBoxLayout 中,而且还需要在 addStretch()
之后使用它们,以便在他们的权利。
对于复杂的布局,我建议使用 Qt 设计器。您可以使用 pyuic5
工具将使用它创建的 ui
文件翻译成 python 代码。
main.py
from PyQt5 import QtWidgets, QtCore
from Ui_SelectPay_Dialog2 import Ui_SelectPay_Dialog2
class SelectPay_Dialog2(QtWidgets.QDialog):
def __init__(self, parent = None):
super().__init__(parent)
ui = Ui_SelectPay_Dialog2()
ui.setupUi(self)
if __name__ == "__main__":
app = QtWidgets.QApplication([])
widget = SelectPay_Dialog2()
widget.show()
app.exec_()
Ui_SelectPay_Dialog2.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_SelectPay_Dialog2(object):
def setupUi(self, SelectPay_Dialog2):
SelectPay_Dialog2.setObjectName("SelectPay_Dialog2")
SelectPay_Dialog2.resize(432, 150)
self.gridLayout = QtWidgets.QGridLayout(SelectPay_Dialog2)
self.gridLayout.setObjectName("gridLayout")
self.label = QtWidgets.QLabel(SelectPay_Dialog2)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.label.sizePolicy().hasHeightForWidth())
self.label.setSizePolicy(sizePolicy)
self.label.setObjectName("label")
self.gridLayout.addWidget(self.label, 0, 0, 1, 1)
self.label_2 = QtWidgets.QLabel(SelectPay_Dialog2)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.label_2.sizePolicy().hasHeightForWidth())
self.label_2.setSizePolicy(sizePolicy)
self.label_2.setObjectName("label_2")
self.gridLayout.addWidget(self.label_2, 1, 0, 1, 1)
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.cb1 = QtWidgets.QCheckBox(SelectPay_Dialog2)
self.cb1.setObjectName("cb1")
self.horizontalLayout.addWidget(self.cb1)
self.cb2 = QtWidgets.QCheckBox(SelectPay_Dialog2)
self.cb2.setObjectName("cb2")
self.horizontalLayout.addWidget(self.cb2)
self.cb3 = QtWidgets.QCheckBox(SelectPay_Dialog2)
self.cb3.setObjectName("cb3")
self.horizontalLayout.addWidget(self.cb3)
self.cb4 = QtWidgets.QCheckBox(SelectPay_Dialog2)
self.cb4.setObjectName("cb4")
self.horizontalLayout.addWidget(self.cb4)
self.cb5 = QtWidgets.QCheckBox(SelectPay_Dialog2)
self.cb5.setObjectName("cb5")
self.horizontalLayout.addWidget(self.cb5)
self.cb6 = QtWidgets.QCheckBox(SelectPay_Dialog2)
self.cb6.setObjectName("cb6")
self.horizontalLayout.addWidget(self.cb6)
self.gridLayout.addLayout(self.horizontalLayout, 1, 1, 1, 1)
self.label_3 = QtWidgets.QLabel(SelectPay_Dialog2)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.label_3.sizePolicy().hasHeightForWidth())
self.label_3.setSizePolicy(sizePolicy)
self.label_3.setObjectName("label_3")
self.gridLayout.addWidget(self.label_3, 2, 0, 1, 1)
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.input_date_text1 = QtWidgets.QLineEdit(SelectPay_Dialog2)
self.input_date_text1.setObjectName("input_date_text1")
self.horizontalLayout_2.addWidget(self.input_date_text1)
self.input_date_text2 = QtWidgets.QLineEdit(SelectPay_Dialog2)
self.input_date_text2.setObjectName("input_date_text2")
self.horizontalLayout_2.addWidget(self.input_date_text2)
spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_2.addItem(spacerItem)
self.gridLayout.addLayout(self.horizontalLayout_2, 2, 1, 1, 1)
self.gridLayout.setRowStretch(0, 1)
self.gridLayout.setRowStretch(1, 1)
self.gridLayout.setRowStretch(2, 1)
self.retranslateUi(SelectPay_Dialog2)
QtCore.QMetaObject.connectSlotsByName(SelectPay_Dialog2)
def retranslateUi(self, SelectPay_Dialog2):
_translate = QtCore.QCoreApplication.translate
SelectPay_Dialog2.setWindowTitle(_translate("SelectPay_Dialog2", "Form"))
self.label.setText(_translate("SelectPay_Dialog2", "test : "))
self.label_2.setText(_translate("SelectPay_Dialog2", "test : "))
self.cb1.setText(_translate("SelectPay_Dialog2", "1week"))
self.cb2.setText(_translate("SelectPay_Dialog2", "2week"))
self.cb3.setText(_translate("SelectPay_Dialog2", "3week"))
self.cb4.setText(_translate("SelectPay_Dialog2", "4week"))
self.cb5.setText(_translate("SelectPay_Dialog2", "5week"))
self.cb6.setText(_translate("SelectPay_Dialog2", "6week"))
self.label_3.setText(_translate("SelectPay_Dialog2", "test : "))
dialog2.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>SelectPay_Dialog2</class>
<widget class="QDialog" name="SelectPay_Dialog2">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>432</width>
<height>150</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QGridLayout" name="gridLayout" rowstretch="1,1,1">
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>test : </string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_2">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>test : </string>
</property>
</widget>
</item>
<item row="1" column="1">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QCheckBox" name="cb1">
<property name="text">
<string>1week</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="cb2">
<property name="text">
<string>2week</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="cb3">
<property name="text">
<string>3week</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="cb4">
<property name="text">
<string>4week</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="cb5">
<property name="text">
<string>5week</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="cb6">
<property name="text">
<string>6week</string>
</property>
</widget>
</item>
</layout>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_3">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>test : </string>
</property>
</widget>
</item>
<item row="2" column="1">
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLineEdit" name="input_date_text1"/>
</item>
<item>
<widget class="QLineEdit" name="input_date_text2"/>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>