Qt Creator创建一个用户登录界面

目录

1 界面设计

2 代码

2.1 登录界面

2.2 注册界面

2.3 登陆后的界面

3 完整资源


        这里主要记录了如何使用Qt Creator创建一个用户登录界面,能够实现用户的注册和登录功能,注册的用户信息存储在了一个文件之中,在登录时可以比对登录信息和文件存储信息,已确认用户是否存在,如果不存在也可以通过注册功能进行注册。

1 界面设计

主要分为3个界面:登录界面、注册界面、登录后的界面

2 代码

2.1 登录界面

登录界面命名对于文件为widget.h、widget.c

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <form.h>
#include <form01.h>

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Form *form = new Form();         // define a object
    Form01 *form01 = new Form01();   // Login

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

private slots:
    void on_pushButton_clicked();

    void on_pushButton_2_clicked();

    void on_pushButton_3_clicked();

private:
    Ui::Widget *ui;
};
#endif // WIDGET_H

widget.c

#include "widget.h"
#include "ui_widget.h"
#include "QDebug"
#include "main.h"
#include "QDir"
#include "QMessageBox"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    connect(this->form, &Form::BackSig, this, [=](){
       this->form->hide();
       this->show();
    });

}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_clicked()
{
    // this->hide();
    // this->close();
    qDebug() << "Change page to Login";

    // gain context about lineEdit and judge and ...
    QString path = QDir::currentPath(); // 获取当前工程所在路径

    std::string user_pwd;  //
    std::string part1;   // useranme
    std::string part2;   // password
    int flag = 1;

    std::ifstream infile("/root/QT_developer/File/user_table.txt");
        if (!infile.is_open()) {
            std::cerr << "Unable to open file!" << std::endl;
        }//

    std::string line;

    QString In_username = ui->lineEdit->text();
    QString In_password = ui->lineEdit_2->text();
    part1 = In_username.toStdString();
    part2 = In_password.toStdString();
    user_pwd = part1 + ":" + part2;

    if(In_password.isEmpty())
    {
        QMessageBox::information(this, "Tips", "In_password is empty!");
    }else if(In_username.isEmpty())
    {
        QMessageBox::information(this, "Tips", "In_usename is empty!");
    }else
    {
        while (std::getline(infile, line)) {  // gain data on a line
            if(user_pwd == line)
            {
                flag = 0;
                infile.close();
                this->close();

                ui->lineEdit->clear();
                ui->lineEdit_2->clear();
                QMessageBox::information(this, "Tips", "Login success!");
                In_username.clear();
                In_password.clear();
                form01->show();
                break;
            }
        }
        if(flag == 1)
        {
            ui->lineEdit->clear();
            ui->lineEdit_2->clear();
            QMessageBox::information(this, "Tips", "username or password is error!");
            In_username.clear();
            In_password.clear();
        }
    }
}

void Widget::on_pushButton_2_clicked()
{
    // this->hide();
    // this->close();
    qDebug() << "Change page to Register";
    form->show();
}

void Widget::on_pushButton_3_clicked()
{
    this->close();
    qDebug() << "Quit";
}

2.2 注册界面

注册界面命名对于文件为form.h、form.c

form.h

#ifndef FORM_H
#define FORM_H

#include <QWidget>

namespace Ui {
class Form;
}

class Form : public QWidget
{
    Q_OBJECT

public:
    explicit Form(QWidget *parent = nullptr);
    ~Form();

public:
    void Open_file();

private slots:
    void on_pushButton_2_clicked();

    void on_pushButton_clicked();

private:
    Ui::Form *ui;

signals:
    void BackSig();  // define a signal without arg.
};

#endif // FORM_H

form.c

#include "form.h"
#include "ui_form.h"
#include "qdebug.h"
#include "widget.h"
#include "QLineEdit"
#include "QMessageBox"
#include "main.h"
#include "QDir"

Form::Form(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Form)
{
    ui->setupUi(this);
    // this->show();

}

Form::~Form()
{
    delete ui;
}

void Form::on_pushButton_2_clicked()
{
    qDebug() << "Back";
    emit this->BackSig();

}

void Form::on_pushButton_clicked()
{
    ///

    QString path = QDir::currentPath(); // 获取当前工程所在路径
    std::size_t found; // 查找冒号的位置
    std::string user_pwd;  // Concatenated username and password
    std::string part1;   // useranme
    std::string part2;   // password
    std::string line;
    int flag = 1;

    std::ofstream outfile; // ready for writing

    // std::ifstream infile(path.toStdString() + "user_table.txt");
    std::ifstream infile("/root/QT_developer/File/user_table.txt");  // Absolute path
        if (!infile.is_open()) {              // Determine whether the opening is successful
            std::cerr << "Unable to open file!" << std::endl;
        }


    ///
    // gain data
    QString username = ui->lineEdit->text();
    QString password = ui->lineEdit_2->text();
    QString password_firm = ui->lineEdit_3->text();

    // pan duan yong hu ming shi fou chong fu
    if(username.isEmpty())
    {
        qDebug() << "username can't is empty";
        QMessageBox::information(this, "Tips", "username can't is empty");
    }else if(password.isEmpty()){
        QMessageBox::information(this, "Tips", "password can't is empty");
    }else if(password_firm.isEmpty())
    {
        QMessageBox::information(this, "Tips", "password_firm can't is empty");
    }else{
        // judge
        if(password != password_firm)
        {
            ui->lineEdit->clear();  // clear
            ui->lineEdit_2->clear();
            ui->lineEdit_3->clear();
            QMessageBox::information(this, "Tips", "password != password_firm!");
            username.clear();    // clear
            password.clear();
            password_firm.clear();

        }else{
            while (std::getline(infile, line)) {  // gain data on a line
                found = line.find(':');   // find :
                if (found != std::string::npos) {
                    part1 = line.substr(0, found); // 从开始到冒号前的部分
                    qDebug() << "part1-username: ";
                    cout << "part1-username: " << part1;
                }
                //
                if(QString::fromStdString(part1) == username)
                {
                    flag = 0;
                    infile.close();

                    ui->lineEdit->clear();
                    ui->lineEdit_2->clear();
                    ui->lineEdit_3->clear();
                    QMessageBox::information(this, "Tips", "username has been exist!");
                    username.clear();
                    password.clear();
                    password_firm.clear();
                    break;
                }
            }
            if(flag == 1){
                QMessageBox::information(this, "Tips", "Register success!");
                part1 = username.toStdString();
                part2 = password.toStdString();
                user_pwd = part1 + ":" + part2;
                outfile.open("/root/QT_developer/File/user_table.txt", ios::in | std::ios::out | std::ios::app);
                outfile << user_pwd << endl;
                outfile.close();

                ui->lineEdit->clear();
                ui->lineEdit_2->clear();
                ui->lineEdit_3->clear();
                username.clear();
                password.clear();
                password_firm.clear();
            }


        }
    }



}

2.3 登陆后的界面

登录后的界面命名对于文件为form01.h、form01.c

form01.h

#ifndef FORM01_H
#define FORM01_H

#include <QWidget>

namespace Ui {
class Form01;
}

class Form01 : public QWidget
{
    Q_OBJECT

public:
    explicit Form01(QWidget *parent = nullptr);
    ~Form01();

private:
    Ui::Form01 *ui;
};

#endif // FORM01_H

form01.c

#include "form01.h"
#include "ui_form01.h"

Form01::Form01(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Form01)
{
    ui->setupUi(this);
}

Form01::~Form01()
{
    delete ui;
}

3 完整资源

按照以上代码就能实现,如果有需要这是完整代码。也可以私我。

https://download.csdn.net/download/qq_51458770/89492862

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/752906.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

大厂程序员上班猝死成常态?

大家好&#xff0c;我是瑶琴呀&#xff0c;拥有一头黑长直秀发的女程序员。 近日&#xff0c;连续看到大厂程序员猝死、低血糖晕倒的新闻&#xff0c;同为程序员感到很难受。互联网加班成常态这是既定事实&#xff0c;尤其在这个内卷严重、经济不景气的环境中&#xff0c;加班…

actual combat 31 —— 多级表头excel导出

设置模板占位符 &#xff08;模板占位符表头不带点&#xff0c;非表头数据行带点&#xff0c;举例{.ago}&#xff0c;{ago}&#xff09;引入easyExcel依赖 <dependency><groupId>com.alibaba</groupId><artifactId>easyexcel</artifactId><v…

【深度学习】图形模型基础(1):使用潜在变量模型进行数据分析的box循环

1.绪论 探索数据背后的隐藏规律&#xff0c;这不仅是数据分析的艺术&#xff0c;更是概率模型展现其威力的舞台。在这一过程中&#xff0c;潜在变量模型尤为关键&#xff0c;它成为了数据驱动问题解决的核心引擎。潜在变量模型的基本理念在于&#xff0c;那些看似复杂、杂乱无…

uniapp加载打点点效果

uniapp加载打点点效果 背景实现思路代码实现尾巴 背景 为了增加系统的交互性&#xff0c;我们在加载数据时通常会增加一些loading动效&#xff0c;但是在某些场景下只需要一些简单文字提醒。比如说使用【加载中】或者【loading】等字段&#xff0c;但是写静态的字符又显得交互…

新手必备!短视频剪辑常用的18个技巧——剪映篇

导入素材&#xff1a;这里我们可以选择自己拍摄好的素材&#xff08;图片、视频或录制好的音频&#xff09;&#xff0c;按照顺序导入剪辑区剪辑。这一步是剪辑的基础&#xff0c;确定剪辑的大体思路与成片框架&#xff01;别忽略了&#xff0c;剪映官方素材库提供的素材&#…

Windows宝塔面板部署ThinkPHP8.0创建Vue项目案例

安装ThinkPHP8.0 登录宝塔面板&#xff0c;创建一个站点。 输入composer代码&#xff0c;执行完成后自动创建TP目录 composer create-project topthink/think tp 网站目录设置为tp&#xff0c;运行目录设置为public 设置PHP版本为8.0以上&#xff0c;不然会出现下面的报错代…

中科驭数第三代DPU芯片K2-Pro,专为数据中心打造的“六边形战士”

近日&#xff0c;中科驭数重磅发布第三代DPU芯片K2-Pro&#xff0c;是国内首颗面向量产的全功能芯片&#xff01; K2-Pro采用自主研发的Kernel Processing Unit架构&#xff0c;集网络、存储、安全及计算等多业务卸载功能于一体&#xff0c;包处理速率翻倍至80Mpps&#xff0c…

vue3+ts+vite项目报错:找不到名称“GC”。ts-plugin(2304)

GC变量通过script标签引入的第三方引入&#xff0c;但是ts-plugin并不知道&#xff0c;需要明确声明这个变量的类型 /// <reference types"vite/client" />declare module "*.vue" {import type { DefineComponent } from "vue";// eslint…

代表与民众的联系如何通过数字人大平台加强?正宇软件有方法

在数字时代的大潮中&#xff0c;数字中国建设已成为国家战略&#xff0c;数字人大平台作为战略中的组成部分&#xff0c;正逐步展现出其独特价值和重要作用。随着国家政策的引导与推动&#xff0c;数字人大平台不仅为人大代表履职提供了新工具&#xff0c;更为加强人大代表与民…

ctfshow web入门 sqli-labs web517--web524

web517 注入点id ?id-1’union select 1,2,3– 确认是否能够注入 ?id-1union select 1,database(),3-- 爆出库名 security爆出表名 ?id-1union select 1,(select group_concat(table_name) from information_schema.tables where table_schemasecurity),3-- emails,refer…

墨刀原型-单选按钮场景交互

画原型过程中&#xff0c;会遇到单选或多选的交互场景 这时就可以直接在基础组件部分&#xff0c;拉取单选按钮直接使用&#xff0c;只需要完成对应的交互事件就可实现交互 首先先说单选按钮实现交互 拉取一个单选组件&#xff0c;右侧可调整样式尺寸&#xff0c;在选项部分&…

生命在于学习——Python人工智能原理(2.5.1)

五、Python的类与继承 5.1 Python面向对象编程 在现实世界中存在各种不同形态的事物&#xff0c;这些事物之间存在各种各样的联系。在程序中使用对象来映射现实中的事物&#xff0c;使用对象之间的关系描述事物之间的联系&#xff0c;这种思想用在编程中就是面向对象编程。 …

【数据结构】线性表之《队列》超详细实现

队列 一.队列的概念及结构二.顺序队列与链队列1.顺序队列2.链队列 三.链队列的实现1.创建队列2.初始化队列3.入队4.出队5.获取队头元素6.获取队尾元素7.队列的大小8.队列的判空9.清空队列10.销毁队列 四.队列的盲区五.模块化源代码1.Queue.h2.Queue.c3.test.c 六.栈和队列必做O…

解题思路:LeetCode 第 209 题 “Minimum Size Subarray Sum“

解题思路&#xff1a;LeetCode 第 209 题 “Minimum Size Subarray Sum” 在这篇博文中&#xff0c;我们将探讨如何使用 Swift 解决 LeetCode 第 209 题 “Minimum Size Subarray Sum”。我们会讨论两种方法&#xff1a;暴力法和滑动窗口法&#xff0c;并对这两种方法的时间复…

confluence集成LDAP

一、confluence的权限管理 在集成前&#xff0c;我们必须得知道confluence自身的权限管理是如何做的。 用户组对应空间权限&#xff0c;用户组可以是一个项目&#xff0c;也可以是一个部门或组。 一个用户组里的用户&#xff0c;可以读写本空间的页面&#xff0c;而把其他组隔离…

“文本比对基础:最短编辑距离算法的原理与实现“

最短编辑距离 给定两个字符串 &#x1d434; 和 &#x1d435;&#xff0c;现在要将 &#x1d434; 经过若干操作变为 &#x1d435;&#xff0c;可进行的操作有&#xff1a; 删除–将字符串 &#x1d434; 中的某个字符删除。插入–在字符串 &#x1d434; 的某个位置插入某…

有没有将音频转文字的app?盘点5款高效的音频转文字工具

在职场的快节奏生活中&#xff0c;时间就是金钱&#xff0c;效率就是生命。 我们常常在会议中奋笔疾书&#xff0c;却错过了关键的讨论&#xff1b;在电话会议中努力记忆要点&#xff0c;却难以捕捉每一个细节。 但别担心&#xff0c;有一种工具能让我们摆脱这些困扰——音频…

靶机渗透之DC-8

一、信息收集 扫一下子网段&#xff0c;发现靶机ip为192.168.145.130。 nmap -sP 192.168.145.* 进一步进行端口、系统等信息扫描&#xff0c;开放的端口为80、22&#xff0c;中间件为apache。 nmap -sT -T4 -sV -O -sC -p1-65535 192.168.145.130 再扫一下网站目录&#xf…

【方法分享】如何使用WinRAR将文件夹里的每个文件压缩

不知道大家是否会遇到这种情况&#xff0c;将文件夹内的多个文件或文件夹压缩成一个个压缩包文件&#xff0c;这种情况除了将文件夹中的文件一个个压缩&#xff0c;还有什么批量操作的方法呢&#xff1f;今天分享使用WinRAR批量压缩文件到每个单独的文件夹的方法。 方法如下&a…

一键vmp加固apk aar aab

简介 最近工作需要基于vpm加固封装了一套一键加固的方案,可以给apk aar aab文件实现一键加固&#xff0c;把所有的dex内容都封装到so库里面&#xff0c;加大了反编译破解的难度。 环境配置: 需要配置环境变量: ANDROID_NDK_HOME ANDROID_SDK_HOME CMAKE_PATH JAVA_HOME使用方…