[分布式即时通讯系统] 注册类完善

我们在qss里添加err_tip样式,根据不同的状态做字体显示

cpp 复制代码
#err_tip[state='normal']{
   color: green;
}
#err_tip[state='err']{
   color: red;
}

接下来项目中添加global.h和global.cpp文件,global.h声明repolish函数,global.cpp用来定义这个函数。

.h中的声明

cpp 复制代码
#ifndef GLOBAL_H
#define GLOBAL_H
#include <QWidget>
#include <functional>
#include "QStyle"

extern std::function<void(QWidget*)> repolish;

#endif // GLOBAL_H

.cpp中的定义

cpp 复制代码
#include "global.h"

std::function<void(QWidget*)> repolish =[](QWidget *w){
    w->style()->unpolish(w);
    w->style()->polish(w);
};

这里要注意的是,因为我们的global.h文件会被包含在多个.cpp文件中,因此我们的repolish包装器的声明和定义需要分开来,否则在链接的时候会报重定义的错。

extern 关键字用于声明一个变量或函数是在另一个文件或同一个文件的其他位置定义的。

在Register的构造函数中添加样式设置。

cpp 复制代码
ui->err_tip->setProperty("state","normal");
repolish(ui->err_tip);

接下来实现获取验证码的逻辑,ui里关联get_code按钮的槽事件,并实现槽函数

cpp 复制代码
void RegisterDialog::on_get_code_clicked()
{
    // 检查用户输入是否正确
    // 验证邮箱的正则表达式
    auto email = ui->email_edit->text();
    QRegularExpression regex(R"((\w+)(\.|_)?(\w*)@(\w+)(\.(w+))+)");
    bool match = regex.match(email).hasMatch();
    if (match) {
        // 发送http验证码
    }
    else {
        // 提示用户邮箱格式不正确,采用tr函数可以实现转语言
        showTip(tr("邮箱格式不正确"), false);
    }
}

在RegisterDialog中添加showTip函数

cpp 复制代码
void RegisterDialog::showTip(QString str, bool b_ok)
{
    if(b_ok){
        ui->err_tip->setProperty("state","err");
    }else{
        ui->err_tip->setProperty("state","normal");
    }
    ui->err_tip->setText(str);
    repolish(ui->err_tip);
}
相关推荐
XLYcmy2 小时前
全链路验证测试系统:一个针对智能代理(Agent)系统全链路能力的自动化验证脚本
分布式·python·http·网络安全·ai·llm·agent
phltxy11 小时前
HAProxy安装与RabbitMQ负载均衡配置
分布式·rabbitmq·负载均衡
jiayong2312 小时前
Kafka 高吞吐消息链路常见面试问题及详细解答
分布式·面试·kafka
卷毛迷你猪13 小时前
快速实验篇(A2-2)数据清洗规则修正与多语言实现验证
hadoop·分布式
业精于勤_荒于稀13 小时前
登录鉴权-ai
分布式
Kurisu57514 小时前
深度拆解:从 CAP 定理到 Raft 协议的分布式一致性演进
分布式
kuokay14 小时前
深入理解 LLM 分布式训练全栈:从硬件到 LLaMA-Factory
分布式·llama·deepspeed·fsdp·llama-factory·accelerate
Java 码思客15 小时前
【Redis分布式缓存实战】第2章 Redis核心数据结构与业务实战场景
redis·分布式·缓存
Rick199316 小时前
Redis 分布式锁 + 部署模式
redis·分布式
phltxy1 天前
RabbitMQ集群搭——多机多节点与单机多节点
分布式·rabbitmq·ruby