husky1r 2023-03-20 19:24 采纳率: 66.7%
浏览 35
已结题

PyQt5怎么使QFormLayout居中?

怎么使QFormLayout居中啊?

from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QLineEdit, QVBoxLayout, QLabel, QHBoxLayout, QFormLayout
from PyQt5.QtGui import QPalette, QBrush, QPixmap
from PyQt5.QtCore import Qt
import sys


class LoginUI(QWidget):
    def __init__(self):
        super(LoginUI, self).__init__()
        # 设置固定大小700*600
        self.setFixedSize(1100, 700)
        # 设置背景图片
        self.setObjectName("RegisterUi")
        self.setStyleSheet("#RegisterUi{border-image:url(./assert/LoginBackgroundImageWidth.png)}")
        self.setAttribute(Qt.WA_StyledBackground)
        # 设置登录账户
        self.accountLineEdit = QLineEdit()
        self.accountLineEdit.setObjectName("RegisterAccount")
        self.accountLineEdit.setStyleSheet("#RegisterAccount{background-color:rgba(255, 255, 255, 0.2)}")
        self.accountLineEdit.setFixedSize(300, 50)
        self.accountLabel = QLabel()
        self.accountLabel.setText("账户:")
        self.accountLabel.setObjectName("AccountLabel")
        self.accountLabel.setStyleSheet("#AccountLabel{color:rgba(255,255,255,1);font-size:25px}")
        self.accountLabel.setFixedSize(70, 50)
        # 设置登录密码
        self.passwordLineEdit = QLineEdit()
        self.passwordLineEdit.setEchoMode(QLineEdit.Password)
        self.passwordLineEdit.setObjectName("RegisterPassword")
        self.passwordLineEdit.setStyleSheet("#RegisterPassword{background-color:rgba(255, 255, 255, 0.2)}")
        self.passwordLineEdit.setFixedSize(300, 50)
        self.passwordLabel = QLabel()
        self.passwordLabel.setText("密码:")
        self.passwordLabel.setObjectName("PasswordLabel")
        self.passwordLabel.setStyleSheet("#PasswordLabel{color:rgba(255,255,255,1);font-size:25px}")
        self.passwordLabel.setFixedSize(70, 50)
        # 设置登录按钮
        self.registerButton = QPushButton()
        self.registerButton.setText("登录(Log in)")
        self.registerButton.setObjectName("RegisterButton")
        self.registerButton.setStyleSheet("#RegisterButton{background-color:rgba(255, 255, 255, 0.5)}")
        self.registerButton.setFixedSize(300, 50)
        # 设置布局
        # 水平布局
        self.formLayout = QFormLayout()
        self.formLayout.setLabelAlignment(Qt.AlignRight)
        self.formLayout.addRow(self.accountLabel, self.accountLineEdit)
        self.formLayout.setSpacing(50)
        self.formLayout.addRow(self.passwordLabel, self.passwordLineEdit)
        # 垂直布局
        self.vlayout = QVBoxLayout()
        self.vlayout.addSpacing(200)
        self.vlayout.addLayout(self.formLayout)
        self.vlayout.addWidget(self.registerButton, 0, Qt.AlignCenter)
        self.vlayout.addSpacing(50)

        self.setLayout(self.vlayout)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    main = LoginUI()
    main.show()
    sys.exit(app.exec_())

img


布局中,怎么设置啊?

  • 写回答

2条回答 默认 最新

  • Leodong. 2023-03-20 19:47
    关注

    可以使用QHBoxLayout来包含QFormLayout和两个空白的QSpacerItem,然后使用addStretch来让QHBoxLayout将QFormLayout居中。


    如果以上回答对您有所帮助,望采纳~谢谢

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 3月28日
  • 已采纳回答 3月20日
  • 创建了问题 3月20日

悬赏问题

  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 Macbookpro 连接热点正常上网,连接不了Wi-Fi。
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题