首页 > 其他分享 >【Qt6.2.4】qml 实现登录注册及显示详情demo

【Qt6.2.4】qml 实现登录注册及显示详情demo

时间:2022-08-28 17:55:47浏览次数:76  
标签:userName Qt6.2 settings demo qml text import userPass id

参考

环境

环境 版本
windows 10
QT 6.2.4
Qt Creator 8.0.1 (Community)
qmake
  • signal 使用信号和槽实现事件监听、参数传递
  • 父组件实现函数,子组件调用,实现复用
  • anchors 锚点实现节点位置调整
  • Settings 实现数据保存本地与读取
  • Popup 实现弹出框提醒

效果展示

项目结构截图

image

登陆注册详情页展示

image

image

image

代码

  1. main.qml
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15
import Qt.labs.settings

Window {
    id: mainWindow
    width: 640
    height: 480
    visible: true
    title: qsTr("主窗口")
    // 点击登录处理函数
    function onLoginChange(userName,userPass){
        console.log(userName,userPass)
    }
    // 点击退出处理函数
    function onAppExit(){
         mainWindow.close()
    }
    LoginWindow {
        id: loginPage
        visible: true
        anchors.centerIn: parent
       // 接受信号第一种(多处调用推荐):可以调用公共方法,但是接收参数要与信号一致
      //  onLoginChange:  mainWindow.onLoginChange(name,password)
        // 接受信号第二种(只在这里使用的方法推荐这种):匿名函数方式,参数可以不与信号参数一致
        onLoginChange:  function(userName,userPass){
            loginPage.visible = false
            userInfoPage.visible = true
        }
        // 切换到注册界面
        onChangePageToReg: {
            loginPage.visible = false
            regUserPage.visible = true
        }
        // 接受信号
        onAppExit: mainWindow.onAppExit()
    }
    UserInfo {
        id: userInfoPage
        visible: false
        anchors.centerIn: parent
        // 切换到登录界面
        onChangePageToLogin: {
            userInfoPage.visible = false
            loginPage.visible = true
        }
    }

    RegUser {
        id: regUserPage
        visible: false
        anchors.centerIn: parent
        // 接受信号
        onAppExit: mainWindow.onAppExit()
        // 切换到登录界面
        onChangePageToLogin: {
            regUserPage.visible = false
            loginPage.visible = true
        }
    }
}

  1. LoginWindow.qml
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15
import Qt.labs.settings


Rectangle{
    id: loginPage
    width: 300
    height: 120
    // 发送登录信号
    signal loginChange()
    // 发送切换到reg注册界面信号
    signal changePageToReg()
    // 退出信号
    signal appExit()
    // 配置
    Settings {
        id: settings
        fileName: "./config.ini"
        property string userName
        property string userPass

    }

    Grid{
        id: userInfoList
        columns: 2
        rows : 2
        spacing: 10
        anchors.horizontalCenter:loginPage.horizontalCenter
        topPadding: 10
        Text{
            text: "用户名:"
        }
        TextField {
            id:userName
        }
        Text{
            text: "密码:"
        }
        TextField {
            id:userPass
            echoMode: TextField.Password
        }
    }
    Popup {
        id: loginMessageBox
        visible: false
        anchors.centerIn: parent
        width: parent.width
        height: 40
        modal: true
        Text{
            id: loginMessageBoxText
            anchors.centerIn: parent
            text:"出错啦"
            width: parent.width
        }
    }
    Row{
        spacing: 10
        anchors.bottom: loginPage.bottom
        anchors.horizontalCenter:loginPage.horizontalCenter
        Button{
            text:"登录"
            onClicked:{
                if(settings.userName !== userName.text || settings.userPass !== userPass.text){
                    loginMessageBox.visible = true
                    loginMessageBoxText.text = "账号密码错误或者不存在该用户";
                    return console.log(loginMessageBoxText.text)
                }
                // 发送登录信号
                loginChange()
            }
        }
        Button{
            text:"切换注册"
            onClicked:{
                // 发送切换到reg注册界面信号
                changePageToReg()
            }
        }
        Button{
            text:"退出登录"
            onClicked:{
                // 发送退出信号
                appExit()
            }
        }
    }

}


  1. RegUser.qml
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15
import Qt.labs.settings

Rectangle{
    id: regUserPage
    width: 300
    height: 120
    // 切换登录
    signal changePageToLogin()
    // 退出信号
    signal appExit()
    // 配置
    Settings {
        id: settings
        fileName: "./config.ini"
        property string userName
        property string userPass
    }
    //
    Grid{
        id: regUserInfoList
        columns: 2
        rows : 4
        spacing: 10
        anchors.horizontalCenter:regUserPage.horizontalCenter
        topPadding: 10
        Text{
            text: "用户名:"
        }
        TextField {
            id: userName
        }
        Text{
            text: "密码:"
        }
        TextField {
            id: userPass
            echoMode: TextField.Password
        }
        Text{
            text: "性别:"
        }
        TextField {

        }
    }
    Row{
        spacing: 10
        anchors.bottom: regUserPage.bottom
        anchors.horizontalCenter:regUserPage.horizontalCenter
        Button{
            text:"注册"
            onClicked:{
                console.log(userName, userPass);
                settings.userName = userName.text
                settings.userPass = userPass.text
                changePageToLogin()
            }
        }
        Button{
            text:"切换登录"
            onClicked:{
                changePageToLogin()
            }
        }
        Button{
            text:"退出登录"
            onClicked:{
                // 发送退出信号
                appExit()
            }
        }
    }

}


  1. UserInfo.qml
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15
import Qt.labs.settings

Rectangle{
    id: userInfoPage
//    border.width: 5
//    border.color: "black"
    width: 300
    height: 120
    // 切换登录
    signal changePageToLogin()
    // 配置
    Settings {
        id: settings
        fileName: "./config.ini"
        property string userName
        property string userPass
    }
    Grid{
        id: userInfoList
        columns: 2
        rows : 4
        spacing: 10
        anchors.horizontalCenter:userInfoPage.horizontalCenter
        topPadding: 10
        Text{
            text: "用户名:"
        }
        TextField {
            id: userName
            text:settings.userName
        }
        Text{
            text: "密码:"
        }
        TextField {
            id: userPass
            text:settings.userPass
        }
        Text{
            text: "性别:"
        }
        TextField {

        }
    }
    Row{
        spacing: 10
        anchors.bottom: userInfoPage.bottom
        anchors.horizontalCenter:userInfoPage.horizontalCenter
        Button{
            text:"更新"
            onClicked:{
                console.log(userName, userPass);
                settings.userName = userName.text
                settings.userPass = userPass.text
                changePageToLogin()
            }
        }
        Button{
            text:"退出登录"
            onClicked:{
                changePageToLogin();
            }
        }
    }

}


标签:userName,Qt6.2,settings,demo,qml,text,import,userPass,id
From: https://www.cnblogs.com/xiaqiuchu/p/16633239.html

相关文章

  • 保存一段qml里使用opengl shader的代码,由于多平台原因暂时用不了
    RowLayout{Layout.fillWidth:trueheight:60......
  • java helloworld demo
    大二的时候写过web仅限于idea配合springboot,学习的时候需要写个javademo或者算法,居然不知道怎么写了首先创建一个文件夹,写上你的代码,因为是demo,所以不......
  • ubuntu22.04+qt6.2安装步骤
    chmod+xqt-unified-linux-x64-4.4.1-online.runsudoaptinstallvimsudoaptinstallnet-toolssudoaptinstallopenssh-serversudoapt-getinstallbuild-essen......
  • 17 - Docker来部署flaskDemo项目
    README.md文件内容:#flaskDemo本接口项目的技术选型:Python+Flask+MySQL+Redis,通过Python+Flask来开发接口使用MySQL来存储用户信息使用Redis用于存储token目......
  • demo实例
    创建实例简述小程序初始文件结构创建完小程序后,系统会自动创建一系列初始目录。接下来分别介绍一下它们。pages文件夹:用来存放小程序中的页面文件,每个小程序页面都会在......
  • jssip3.9.1的demo,webphone网页电话
    用的目前最新的3.9.1版本,全版本在这里:https://jssip.net/download/releases/https://github.com/versatica/JsSIP 代码:<!DOCTYPEhtml><htmllang="en"><head......
  • “判断性别”Demo需求分析和初步设计(中)
    大家好~我开设了“深度学习基础班”的线上课程,带领同学从0开始学习全连接和卷积神经网络,进行数学推导,并且实现可以运行的Demo程序线上课程资料:本节课录像回放加QQ群,获得......
  • CountDownLatch demo演示裁判和选手赛跑
    #CountDownLatchdemo演示裁判和选手赛跑packagecom.example.core.mydemo;importjava.util.concurrent.CountDownLatch;importjava.util.concurrent.ExecutorServ......
  • CountDownLatch demo演示数据分片多线程处理
    #CountDownLatchdemo演示数据分片多线程处理packagecom.example.core.mydemo;importorg.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;import......
  • Javascript日期、城市选择器(demo)
    1<htmlxmlns="http://www.w3.org/1999/xhtml">2<head>3<title>精美js日期选择器,js省市选择器-何问起</title>4<linktype="text/css"rel="Stylesheet"hre......