1、前言
记录一下QML设计登录界面,巩固自己对于QML的学习,方便自己日后回顾,也可以给有需要的人提供帮助。每个目录都是知识点,大家可以按照目录逐步浏览,也可以点击目录跳转到所需部分。 |
2、应用程序窗口
设计登录窗口
ApplicationWindow{
id:window
width: 540
height: 420
visible: true
title: qsTr("登录")
Rectangle{
anchors.fill: parent
gradient: Gradient{
GradientStop{position: 0.0; color: "#8EC5FC"}
GradientStop{position: 1.0;color: "#E0C3FC"}
}
}
}
3、用户输入区域
ColumnLayout {
spacing: 5
Text {
text: "用户名"
color: "#34495e"
Layout.leftMargin: 5
}
TextField {
id: usernameField
placeholderText: "请输入用户名"
Layout.fillWidth: true
Layout.preferredHeight: 45
background: Rectangle {
radius: 8
border.color: usernameField.focus ? "#3498db" : "#bdc3c7"
border.width: 1
}
onAccepted: loginButton.clicked()
}
}
4、密码输入区域
ColumnLayout {
spacing: 5
Text {
text: "密码"
color: "#34495e"
}
TextField {
id: passwordField
placeholderText: "请输入密码"
echoMode: TextInput.Password
Layout.fillWidth: true
Layout.preferredHeight: 45
background: Rectangle {
radius: 5
border.color: passwordField.focus ? "#3498db" : "#bdc3c7"
border.width: 1
}
onAccepted: loginButton.clicked()
}
}
5、登录按钮
Button {
id: loginButton
text: "登录"
Layout.fillWidth: true
Layout.topMargin: 20
Layout.preferredHeight: 50
contentItem: Text {
text: loginButton.text
color: "white"
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
}
background: Rectangle {
radius: 5
color: loginButton.down ? "#2980b9" : "#3498db"
}
onClicked: {
if(usernameField.text === "" || passwordField.text === ""){
errorMessage.text = "用户名和密码不能为空!"
errorMessage.visible = true
} else {
errorMessage.visible = false
console.log("尝试登录:", usernameField.text)
}
}
}
6、底部注册提示
Text {
Layout.topMargin: 20
anchors {
bottom: parent.bottom
horizontalCenter: parent.horizontalCenter
margins: 20
}
text: "还没有账号?立即注册"
color: "#3498db"
}
7、QML文件
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.1
ApplicationWindow{
id:window
width: 540
height: 420
visible: true
title: qsTr("登录")
Rectangle{
anchors.fill: parent
gradient: Gradient{
GradientStop{position: 0.0; color: "#8EC5FC"}
GradientStop{position: 1.0;color: "#E0C3FC"}
}
}
ColumnLayout {
id: loginBox
anchors.centerIn: parent
spacing: 20
width: parent.width * 0.8
Text {
text: "用户登录"
font.pixelSize: 28
color: "#2c3e50"
Layout.alignment: Qt.AlignHCenter
}
ColumnLayout {
spacing: 5
Text {
text: "用户名"
color: "#34495e"
Layout.leftMargin: 5
}
TextField {
id: usernameField
placeholderText: "请输入用户名"
Layout.fillWidth: true
Layout.preferredHeight: 45
background: Rectangle {
radius: 8
border.color: usernameField.focus ? "#3498db" : "#bdc3c7"
border.width: 1
}
onAccepted: loginButton.clicked()
}
}
ColumnLayout {
spacing: 5
Text {
text: "密码"
color: "#34495e"
}
TextField {
id: passwordField
placeholderText: "请输入密码"
echoMode: TextInput.Password
Layout.fillWidth: true
Layout.preferredHeight: 45
background: Rectangle {
radius: 5
border.color: passwordField.focus ? "#3498db" : "#bdc3c7"
border.width: 1
}
onAccepted: loginButton.clicked()
}
}
Button {
id: loginButton
text: "登录"
Layout.fillWidth: true
Layout.topMargin: 20
Layout.preferredHeight: 50
contentItem: Text {
text: loginButton.text
color: "white"
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
}
background: Rectangle {
radius: 5
color: loginButton.down ? "#2980b9" : "#3498db"
}
onClicked: {
if(usernameField.text === "" || passwordField.text === ""){
errorMessage.text = "用户名和密码不能为空!"
errorMessage.visible = true
} else {
errorMessage.visible = false
console.log("尝试登录:", usernameField.text)
}
}
}
Text {
id: errorMessage
visible: false
color: "#e74c3c"
Layout.alignment: Qt.AlignHCenter
}
Text {
Layout.topMargin: 20
anchors {
bottom: parent.bottom
horizontalCenter: parent.horizontalCenter
margins: 20
}
text: "还没有账号?立即注册"
color: "#3498db"
MouseArea {
anchors.fill: parent
onClicked: console.log("转到注册页面")
cursorShape: Qt.PointingHandCursor
}
}
}
}
8、实现效果

9、总结
以上就是QML设计登录界面的整个过程了,浏览过程中,如若发现错误,欢迎大家指正,有问题的欢迎评论区留言或者私信。最后,如果大家觉得有所帮助,可以点一下赞,谢谢大家!祝大家天天开心,顺遂无虞! |