如何在QWidget中对齐QLineEdit?

问题描述 投票:0回答:1

我有一个小部件,其中包含三个标签和三个lineedit。我希望所有lineedit在最长标签之后直接垂直对齐。

这是我的课程:

class ScaleDisplayWidget(QWidget):
    def __init__(self, parent=None):
        super(ScaleDisplayWidget, self).__init__(parent)
        self.setFixedSize(400, 200)
        self.initUI()
        self.update(0, 0, 0)

    def initUI(self):
        '''
        Setup GUI elements of scale window
        '''
        mainLayout = QVBoxLayout()

        hLayout = QHBoxLayout()
        hLayout.setSpacing(0)

        self.dx_label = QLabel('DX:')
        self.dx_label.setFixedWidth(80)
        self.dx_edit = QLineEdit()
        self.dx_edit.setReadOnly(True)
        self.dx_edit.setFocus(True)
        self.dx_edit.setFixedWidth(150)

        hLayout.addWidget(self.dx_label)
        hLayout.addWidget(self.dx_edit)

        h2Layout = QHBoxLayout()
        h2Layout.setSpacing(0)

        self.dy_label = QLabel('DY:')
        self.dy_label.setFixedWidth(80)
        self.dy_edit = QLineEdit()
        self.dy_edit.setReadOnly(True)
        self.dy_edit.setFocus(True)
        self.dy_edit.setFixedWidth(150)

        h2Layout.addWidget(self.dy_label)
        h2Layout.addWidget(self.dy_edit)

        h3Layout = QHBoxLayout()
        h3Layout.setSpacing(0)

        self.dist_label = QLabel('Distance:')
        self.dist_label.setFixedWidth(80)
        self.dist_edit = QLineEdit()
        self.dist_edit.setReadOnly(True)
        self.dist_edit.setFocus(True)
        self.dist_edit.setFixedWidth(150)

        h3Layout.addWidget(self.dist_label)
        h3Layout.addWidget(self.dist_edit)

        mainLayout.addLayout(hLayout)
        mainLayout.addLayout(h2Layout)
        mainLayout.addLayout(h3Layout)

        self.setLayout(mainLayout)
        self.show()

    def update(self, dx, dy, dist):
        self.dx_edit.setText(str(dx))
        self.dy_edit.setText(str(dy))
        self.dist_edit.setText(str(dist))

在这种情况下,我的目标是使所有的线编辑在距离标签之后直接对齐(也许添加5像素或一些小的填充)。我已经尝试过在所有布局上使用setContentsMargins(0,0,0,0),但它并没有改变任何内容。

python pyqt pyqt5
1个回答
0
投票

改为使用QFormLayout

© www.soinside.com 2019 - 2024. All rights reserved.