【Qt应用】Qt编写简易登录注册界面

目录

引言

一、准备工作

二、设计思路

2.1 登录 

2.2 注册

三、登录功能 

3.1 创建账号与密码输入框 

3.2 设置密码输入框格式

3.2.1 初始

3.2.2 创建一个显示隐藏按钮

3.2.3 信号与槽函数 

3.3 创建登录按钮

3.4 创建可选功能

四、注册功能

4.1 账号和密码输入框

4.2 设置密码输入框格式

4.2.1 设置输入框为密码样式

4.2.2 创建显示与隐藏密码按钮

4.2.3 设置按钮样式

五、优化

5.1 账号密码输入框动态显示个数

5.1.1 创建QLabel

5.1.2 连接信号与槽函数 

5.1.3 槽函数实现 

5.2 设置窗口背景

5.3 实现效果

结语 


引言

在开发桌面应用程序时,登录和注册界面是常见的需求,它们不仅提供了用户身份验证的功能,还是用户体验的重要组成部分。Qt,作为跨平台的C++图形用户界面应用程序开发框架,凭借其丰富的控件集和高效的开发环境,成为实现这类界面的理想选择。本文将详细介绍如何使用Qt编写一个包含登录和注册功能的界面,涵盖准备工作、设计思路、功能实现及测试调试等方面。 

一、准备工作

在开始编写代码之前,确保你的开发环境中已经安装了Qt和相应的Qt Creator IDE。Qt Creator是Qt的官方集成开发环境,提供了代码编辑、项目管理、调试等一系列开发工具。

  1. 下载并安装Qt

    访问Qt官网下载适合你的操作系统的Qt版本。安装时,请确保选择安装Qt库(如Qt Widgets模块)和Qt Creator IDE。

  2. 配置Qt Creator

    安装完成后,打开Qt Creator,通过“工具”->“选项”->“构建和运行”->“Qt版本”来检查Qt版本是否正确配置。同时,确保编译器(如MinGW或MSVC)也已正确设置。

  3. 创建新项目

    在Qt Creator中,选择“文件”->“新建文件或项目”,然后选择“应用程序”下的“Qt Widgets Application”。填写项目名称(如“LoginRegisterApp”),选择保存位置,并选择合适的Qt版本。点击“下一步”进行项目配置,最后点击“完成”创建项目。

二、设计思路

在设计登录和注册界面之前,我们需要明确界面的功能和布局需求。

2.1 登录 

  • 包含账号和密码输入框。
  • 包含一个登录按钮,用于提交登录信息。
  • 包含记住密码和自动登录的可选框功能。
  • 提供登录失败与登录成功提示。

2.2 注册

  • 包含账号、密码、确认密码、邮箱等输入框。
  • 包含一个注册按钮,用于提交注册信息。
  • 验证两次输入的密码是否一致等。
  • 提供注册成功或失败的提示。

为了简化开发,我们可以考虑使用QStackedWidget来在同一个窗口中切换登录和注册界面,或者设计为两个独立的窗口。这里,我们选择使用QStackedWidget来切换界面。

三、登录功能 

3.1 创建账号与密码输入框 

这里创建了一个QGroupBox控件去管理用户名与密码的控件

//创建登录的QGroupBox
QGroupBox *groupbox1 = new QGroupBox(this);
QVBoxLayout *layout1 = new QVBoxLayout(groupbox1);
QLabel *account = new QLabel("账号:", groupbox1);
accountLineEdit = new QLineEdit(groupbox1);
QLabel *password = new QLabel("密码:", groupbox1);
passwordLineEdit = new QLineEdit(groupbox1);

此外,还可以设置两个QLineEdit的占位符文本

// 设置占位符属性(默认文本)
accountLineEdit->setPlaceholderText("请输入账号...");
passwordLineEdit->setPlaceholderText("请输入密码...");

3.2 设置密码输入框格式

3.2.1 初始

初始时密码输入设置为小黑点样式

passwordLineEdit->setEchoMode(QLineEdit::Password);
3.2.2 创建一个显示隐藏按钮

创建一个QPushButton按钮,并对其图标、位置、大小与背景进行设置

// 创建密码显示或隐藏按钮
passwordShow = new QPushButton(this);
// 设置图标
passwordShow->setIcon(QIcon(":/image/show.png"));
// 设置位置与大小
passwordShow->resize(30, 30);
passwordShow->move(350, 85);
// 设置透明背景
passwordShow->setStyleSheet("QPushButton { background-color: transparent; }");
3.2.3 信号与槽函数 

连接按钮的点击信号与槽函数:

// 连接信号与槽函数
connect(passwordShow, &QPushButton::clicked, this, &Widget::passwordShowHide);

槽函数的实现:

//密码输入框密码显示与隐藏槽函数
void Widget::passwordShowHide()
{
    QPushButton *temp = qobject_cast<QPushButton *>(sender());
    QLineEdit *temp2;
    if(temp == passwordShow)
    {
        temp2 = passwordLineEdit;
    }
    else if(temp == passwordShow2)
    {
        temp2 = passwordLineEdit2;
    }
    else
    {
        temp2 = passwordLineEdit3;
    }

    if (temp2->echoMode() == QLineEdit::Password) {
        temp2->setEchoMode(QLineEdit::Normal); // 切换为明文显示
        temp->setIcon(QIcon(":/image/hide.png"));
    } else {
        temp2->setEchoMode(QLineEdit::Password); // 切换回密码显示
        temp->setIcon(QIcon(":/image/show.png"));
    }
}

3.3 创建登录按钮

QPushButton *loginButton = new QPushButton("登录", wid);

 其中wid参数是其父窗口指针 

3.4 创建可选功能

通过创建一个QGroupBox对两个复选框QCheckBox进行管理

//创建信息可选项的QGroupBox
QGroupBox *groupbox2 = new QGroupBox(this);
QHBoxLayout *layout2 = new QHBoxLayout(groupbox2);
//创建复选框
QCheckBox *memory = new QCheckBox("记住密码", groupbox2);
QCheckBox *automate = new QCheckBox("自动登录", groupbox2);
//将创建的复选框加入到布局中
layout2->addWidget(memory);
layout2->addWidget(automate);

四、注册功能

4.1 账号和密码输入框

//创建注册的QGroupBox
QGroupBox *groupbox4 = new QGroupBox(this);
QVBoxLayout *layout4 = new QVBoxLayout(groupbox4);
QLabel *account2 = new QLabel("账号:", groupbox4);
accountLineEdit2 = new QLineEdit(groupbox4);
QLabel *password2 = new QLabel("设置密码:", groupbox4);
passwordLineEdit2 = new QLineEdit(groupbox4);
QLabel *password3 = new QLabel("确认密码:", groupbox4);
passwordLineEdit3 = new QLineEdit(groupbox4);

此外,还可设置占位符文本:

// 设置占位符属性(默认文本)
accountLineEdit2->setPlaceholderText("请输入账号...");
passwordLineEdit2->setPlaceholderText("请输入密码...");
passwordLineEdit3->setPlaceholderText("请再次输入密码...");

4.2 设置密码输入框格式

4.2.1 设置输入框为密码样式
// 设置echoMode为Password,使得输入时显示小黑点或星号
passwordLineEdit2->setEchoMode(QLineEdit::Password);
passwordLineEdit3->setEchoMode(QLineEdit::Password);
4.2.2 创建显示与隐藏密码按钮
// 创建密码显示或隐藏按钮
passwordShow2 = new QPushButton(this);
passwordShow3 = new QPushButton(this);
4.2.3 设置按钮样式
// 设置图标
passwordShow2->setIcon(QIcon(":/image/show.png"));
passwordShow3->setIcon(QIcon(":/image/show.png"));
// 设置位置与大小
passwordShow2->resize(30, 30);
passwordShow2->move(350, 83);
passwordShow3->resize(30, 30);
passwordShow3->move(350, 132);
// 设置透明背景
passwordShow2->setStyleSheet("QPushButton { background-color: transparent; }");
passwordShow3->setStyleSheet("QPushButton { background-color: transparent; }");
// 连接信号与槽函数
connect(passwordShow2, &QPushButton::clicked, this, &Widget::passwordShowHide);
connect(passwordShow3, &QPushButton::clicked, this, &Widget::passwordShowHide);

五、优化

5.1 账号密码输入框动态显示个数

账号密码在输入时应该实时反映给槽函数

5.1.1 创建QLabel

登录界面创建两个QLabel作为个数显示,并对其属性进行设置

//创建两个QLabel作为账号密码输入框的动态显示文本数量
display1 = new QLabel("0/11", accountLineEdit);
display2 = new QLabel("0/8", passwordLineEdit);
//设置对齐方式
display1->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
display2->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
//设置大小
display1->setFixedSize(40, 40);
//设置QLabel的样式
display1->setStyleSheet(QString::fromLocal8Bit("border:none;color: #cccccc;font: 10pt \"微软雅黑\";"));
display2->setStyleSheet(QString::fromLocal8Bit("border:none;color: #cccccc;font: 10pt \"微软雅黑\";"));
//设置QLabel的在QLineEdit中的位置
display1->move(310, -10);
display2->move(310, 2);

注册界面创建三个QLabel作为个数显示,并对其属性进行设置

//创建三个QLabel作为账号密码输入框的动态显示文本数量
display3 = new QLabel("0/11", accountLineEdit2);
display4 = new QLabel("0/8", passwordLineEdit2);
display5 = new QLabel("0/8", passwordLineEdit3);
//设置对齐方式
display3->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
display4->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
display5->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
//设置大小
display3->setFixedSize(40, 40);
display4->setFixedSize(40, 40);
display5->setFixedSize(40, 40);
//设置QLabel的样式
display3->setStyleSheet(QString::fromLocal8Bit("border:none;color: #cccccc;font: 10pt \"微软雅黑\";"));
display4->setStyleSheet(QString::fromLocal8Bit("border:none;color: #cccccc;font: 10pt \"微软雅黑\";"));
display5->setStyleSheet(QString::fromLocal8Bit("border:none;color: #cccccc;font: 10pt \"微软雅黑\";"));
//设置QLabel的在QLineEdit中的位置
display3->move(310, -10);
display4->move(290, -10);
display5->move(290, -10);
5.1.2 连接信号与槽函数 
connect(accountLineEdit, &QLineEdit::textEdited, this, &Widget::accountTextChange);
connect(passwordLineEdit, &QLineEdit::textEdited, this, &Widget::passwordTextChange);

connect(accountLineEdit2, &QLineEdit::textEdited, this, &Widget::accountTextChange);
connect(passwordLineEdit2, &QLineEdit::textEdited, this, &Widget::passwordTextChange);
connect(passwordLineEdit3, &QLineEdit::textEdited, this, &Widget::passwordTextChange);
5.1.3 槽函数实现 

账号输入框文本变化槽函数: 

//登录注册界面的账号输入框文本变化槽函数
void Widget::accountTextChange(const QString& text)
{
    //获取文本变化的输入框的指针
    QLineEdit *temp = qobject_cast<QLineEdit *>(sender());
    QLabel *temp2;
    if(temp == accountLineEdit)
    {
        temp2 = display1;
    }
    else
    {
        temp2 = display3;
    }
    int len = text.length();
    if(len > 11)
    {
        //将文本回退到未超过上限时的样子
        QString text1 = text;
        //移除最后一个字符
        text1.chop(1);
        temp->setText(text1);

        QMessageBox::warning(this, "警告", "账号位数超过上限!");
    }
    else if(len == 11)
    {
        temp2->setText(QString("%1/11").arg(len));
        temp2->setStyleSheet(QString::fromLocal8Bit("color: #1afa29;font: 10pt \"微软雅黑\";"));
    }
    else
    {
        temp2->setText(QString("%1/11").arg(len));
        temp2->setStyleSheet(QString::fromLocal8Bit("color: #FF5050;font: 10pt \"微软雅黑\";"));
    }
}

密码输入框文本变化槽函数:

//登录注册界面的密码输入框文本变化槽函数
void Widget::passwordTextChange(const QString& text)
{
    int len = text.length();
    //获取文本变化的输入框的指针
    QLineEdit *temp = qobject_cast<QLineEdit *>(sender());
    QLabel *temp2;
    if(temp == passwordLineEdit)
    {
        temp2 = display2;
    }
    else if(temp == passwordLineEdit2)
    {
        temp2 = display4;
    }
    else
    {
        temp2 = display5;
    }
    if(len < 8)
    {
        temp2->setText(QString("%1/8").arg(len));
        temp2->setStyleSheet(QString::fromLocal8Bit("color: #FF5050;font: 10pt \"微软雅黑\";"));
    }
    else if(len >= 8 && len <= 16)
    {
        int l = temp2->text().length();
        //if(display2->text().right(1).toInt() != 6)
        if(temp2->text().at(l - 1) != '6')
        {
            temp2->resize(50, 40);
            temp2->move(300, -10);
        }
        temp2->setText(QString("%1/16").arg(len));
        temp2->setStyleSheet(QString::fromLocal8Bit("color: #1afa29;font: 10pt \"微软雅黑\";"));
    }
    else
    {
        //将文本回退到未超过上限时的样子
        QString text1 = text;
        //移除最后一个字符
        text1.chop(1);
        temp->setText(text1);

        QMessageBox::warning(this, "警告", "密码位数超过上限!");
    }
}

5.2 设置窗口背景

QWidget窗口的背景设置可以通过重写paintEvent函数实现:

//重写绘制函数
void Widget::paintEvent(QPaintEvent *event)
{
    QWidget::paintEvent(event);

    //创建QPainter绘制对象
    QPainter painter(this);
    //加载并缩放图片
    QPixmap pixmap(":/image/2.jpg");
    pixmap = pixmap.scaled(this->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation); //保持宽高比并使用平滑变化
    //绘制图片
    painter.drawPixmap(this->rect(), pixmap);
}

5.3 实现效果

结语 

        通过本文,我们详细介绍了如何使用Qt编写一个包含登录和注册功能的界面。从准备工作、设计思路、功能实现等等,我们涵盖了开发过程中的主要步骤。当然,这只是一个基础示例,实际应用中可能需要考虑更多的细节,如用户权限管理、会话保持、界面美化等。

        Qt框架的强大之处在于其丰富的功能和灵活性,你可以根据自己的需求,进一步扩展和定制你的应用程序。例如,你可以使用Qt Charts模块来添加图表功能,使用Qt Multimedia模块来处理音视频数据,或者使用Qt Network模块来实现网络通信等。希望本文能为你提供一个良好的起点,让你在Qt开发的道路上越走越远。

 

  • 13
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值