欢迎加入QQ群:853840665,一块学习讨论
QFormLayout管理输入型控件和关联的标签组成的那些Form表单。
QFormLayout表单布局,其中的控件以两列的形式被布局在表单中。左列包括标签,用作提示输入信息。右列包含输入控件,例如:QLineEdit、QSpinBox等,用于输入数据。再Android中做这种布局一般左侧是用Label,不得不说QT很牛,有这么方便的布局。
下面看一个例子
import sys
from PyQt5.QtWidgets import *
from PyQt5 .QtCore import Qt
class lineEditLearn(QWidget):
def __init__(self, parent = None):
super(lineEditLearn, self).__init__(parent)
self.addUi()
self.setWindowTitle("QFormLayout 学习")
def addUi(self):
#创建一个表单布局
qfl = QFormLayout()
#设置标签右对齐,不设置是默认左对齐
qfl.setLabelAlignment(Qt.AlignRight)
#创建4个单行文本框
normalLineEdit = QLineEdit()
#设置输入文本右对齐——只是练习
normalLineEdit.setAlignment(Qt.AlignRight)
echoLineEdit = QLineEdit()
passwordLineEdit = QLineEdit()
passwordEchoLineEdit = QLineEdit()
#把文本框添加到布局,第一个参数为左侧的说明标签
qfl.addRow("Normal", normalLineEdit)
qfl.addRow("EchoMode", echoLineEdit)
qfl.addRow("Password", passwordLineEdit)
qfl.addRow("PasswordEchoOnEditdit", passwordEchoLineEdit)
#设置提示输入文本
normalLineEdit.setPlaceholderText("请输入密码")
echoLineEdit.setPlaceholderText("请输入密码")
passwordLineEdit.setPlaceholderText("请输入密码")
passwordEchoLineEdit.setPlaceholderText("请输入密码")
#设置显示效果
normalLineEdit.setEchoMode(QLineEdit.Normal)
echoLineEdit.setEchoMode(QLineEdit.NoEcho)
passwordLineEdit.setEchoMode(QLineEdit.Password)
passwordEchoLineEdit.setEchoMode(QLineEdit.PasswordEchoOnEdit)
#把设置的布局加载到窗口
self.setLayout(qfl)
if __name__ == "__main__":
app = QApplication(sys.argv)
win = lineEditLearn()
win.show()
sys.exit(app.exec_())