删除 QLabel 和 QLineEdit 之间的边距

remove margin between QLabel and QLineEdit

我正在尝试创建登录 Window,它在 QLabel 下面有 QLineEdit,但是当前的 QLabel 在 Window 中占用了太多 space,我不知道为什么,这是图片:

QLabel { background-color: red; }

我的代码:

self.text = QLabel("LOGIN")

# self.text.setStyleSheet("QLabel { background-color: red; color : white;margin-top: 50px;margin-bottom: 0px; }")
self.text.setAlignment(Qt.AlignCenter)
# self.text.setContentsMargins(0, 0, 0, 0)
# self.text.setGeometry(QRect(10,10,30,80))

font = QFont("Sans-Serif", 30)
self.text.setFont(font)

form = QLineEdit("Write my name here..")
form.setAlignment(Qt.AlignCenter)
# form.setAlignment(Qt.AlignHCenter)

self.layout = QVBoxLayout()
# self.layout.setContentsMargins(0, 0, 0, 0)
self.layout.setSpacing(0)
self.layout.addWidget(self.text)
self.layout.addWidget(form)
self.layout.addWidget(self.button)
self.setLayout(self.layout)

我没有看到 OP 提供的代码与要从中复制结构的 LOGIN window 之间的关系,因此请从头开始创建以下代码。

from PySide2 import QtCore, QtGui, QtWidgets


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

        self.image_label = QtWidgets.QLabel(alignment=QtCore.Qt.AlignCenter)
        self.login_label = QtWidgets.QLabel(
            text=self.tr("LOG IN"), alignment=QtCore.Qt.AlignCenter
        )
        self.email_username_lineedit = QtWidgets.QLineEdit(
            placeholderText=self.tr("Email or username")
        )
        self.password_lineedit = QtWidgets.QLineEdit(
            placeholderText=self.tr("Password"), echoMode=QtWidgets.QLineEdit.Password
        )
        self.enter_button = QtWidgets.QPushButton(self.tr("Enter"))
        self.forgot_password_label = QtWidgets.QLabel(
            self.tr("Forgot password?"), alignment=QtCore.Qt.AlignCenter
        )

        self.image_label.setPixmap(QtGui.QPixmap("so-icon.png"))

        # font = self.
        font = self.login_label.font()
        font.setPointSize(30)
        self.login_label.setFont(font)

        self.login_label.setSizePolicy(
            QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Fixed
        )
        self.forgot_password_label.setSizePolicy(
            QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Fixed
        )

        lay = QtWidgets.QVBoxLayout(self)
        lay.setSpacing(0)
        lay.addWidget(self.image_label)
        lay.addWidget(self.login_label)
        lay.addWidget(self.email_username_lineedit)
        lay.addWidget(self.password_lineedit)
        lay.addWidget(self.enter_button)
        lay.addWidget(self.forgot_password_label)

        self.resize(320, 480)


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = LoginPage()
    w.show()
    sys.exit(app.exec_())