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

发布于:2024-12-18 ⋅ 阅读:(153) ⋅ 点赞:(0)

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

#err_tip[state='normal']{
   color: green;
}
#err_tip[state='err']{
   color: red;
}

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

.h中的声明

#ifndef GLOBAL_H
#define GLOBAL_H
#include <QWidget>
#include <functional>
#include "QStyle"

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

#endif // GLOBAL_H

.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的构造函数中添加样式设置。

ui->err_tip->setProperty("state","normal");
repolish(ui->err_tip);

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

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函数

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);
}

网站公告

今日签到

点亮在社区的每一天
去签到