Qgridlayout控制小部件的大小

时间:2020-01-17 04:17:13

标签: python user-interface pyqt qgridlayout

我正在尝试将Qgridlayout用于我的GUI。我希望减小Qspin的宽度,如图所示。有什么建议我可以做到吗?谢谢

    layout.addWidget(checkBoxVBG,1,0)
    layout.addWidget(checkBoxIREF,2,0)


    layout.addWidget(boxVBG, 1, 1)
    layout.addWidget(boxIREF, 2, 1)


    layout.addWidget(checkBoxZERO_X,1,2)
    layout.addWidget(checkBox4MOSC,2,2)


    layout.addWidget(boxZERO_X, 1, 3)
    layout.addWidget(box4MOSC, 2, 3)

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以设置固定宽度并向左对齐。

import sys

from PyQt5 import QtCore, QtGui, QtWidgets


class Widget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)

        layout = QtWidgets.QGridLayout(self)

        checkBoxVBG = QtWidgets.QCheckBox("VBG")
        checkBoxIREF = QtWidgets.QCheckBox("IREF")
        boxVBG = QtWidgets.QSpinBox()
        boxIREF = QtWidgets.QSpinBox()
        checkBoxZERO_X = QtWidgets.QCheckBox("ZERO X")
        checkBox4MOSC = QtWidgets.QCheckBox("4MOSC")
        boxZERO_X = QtWidgets.QSpinBox()
        box4MOSC = QtWidgets.QSpinBox()

        for spinbox in (boxVBG, boxIREF, boxZERO_X, box4MOSC):
            spinbox.setFixedWidth(60)

        layout.addWidget(checkBoxVBG, 1, 0)
        layout.addWidget(checkBoxIREF, 2, 0)

        layout.addWidget(boxVBG, 1, 1, alignment=QtCore.Qt.AlignLeft)
        layout.addWidget(boxIREF, 2, 1, alignment=QtCore.Qt.AlignLeft)

        layout.addWidget(checkBoxZERO_X, 1, 2)
        layout.addWidget(checkBox4MOSC, 2, 2)

        layout.addWidget(boxZERO_X, 1, 3, alignment=QtCore.Qt.AlignLeft)
        layout.addWidget(box4MOSC, 2, 3, alignment=QtCore.Qt.AlignLeft)


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())