add button functionality

This commit is contained in:
xyz
2025-07-30 11:38:07 +08:00
parent 01c4baf04d
commit 83f3f4f74e
10 changed files with 623 additions and 26 deletions

View File

@ -1,11 +1,8 @@
#include "selfinfowidget.h"
#include <QCursor>
#include <QGridLayout>
#include "model/datacenter.h"
using namespace model;
SelfInfoWidget::SelfInfoWidget(QWidget *parent)
: QDialog(parent)
@ -20,7 +17,7 @@ SelfInfoWidget::SelfInfoWidget(QWidget *parent)
this->move(QCursor::pos());
//创建布局管理器
QGridLayout* layout = new QGridLayout();
layout = new QGridLayout();
//layout->setSpacing(0);
layout->setHorizontalSpacing(10);
layout->setVerticalSpacing(3);
@ -32,14 +29,12 @@ SelfInfoWidget::SelfInfoWidget(QWidget *parent)
avatarBtn = new QPushButton();
avatarBtn->setFixedSize(75, 75);
avatarBtn->setIconSize(QSize(75, 75));
//avatarBtn->setIcon(QIcon(":/resource/image/defaultAvatar.png"));
avatarBtn->setIcon(QIcon(":/resource/image/defaultAvatar.png"));
avatarBtn->setStyleSheet("QPushButton { border: none; background-color: transparent; }");
QString labelStyle = "QLabel { font-size: 14px; font-weight: 800; }";
QString btnStyle = "QPushButton {border: none; background-color: transparent; }";
btnStyle += "QPushButton:pressed { background-color: rgb(210, 210, 210); }";
QString editStyle = "QLineEdit { border: none; background-color: rgb(255 ,255, 255); border-radius: 5px; padding-left: 2px; }";
QString editStyle = "QLineEdit { border: none; background-color: rgb(250 ,250, 250); color: rgb(0, 0, 0); border-radius: 5px; padding-left: 2px; }";
int height = 30;
@ -118,12 +113,13 @@ SelfInfoWidget::SelfInfoWidget(QWidget *parent)
phoneTag = new QLabel();
phoneTag->setFixedSize(50, height);
phoneTag->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
phoneTag->setText("电话");
phoneTag->setText("邮箱");
phoneTag->setStyleSheet(labelStyle);
phoneLabel = new QLabel();
phoneLabel->setFixedHeight(height);
phoneLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
//phoneLabel->setStyleSheet("QLabel {background-color: rgb(255, 255, 255); }");
phoneModifyBtn = new QPushButton();
phoneModifyBtn->setFixedSize(70, 25);
@ -134,7 +130,8 @@ SelfInfoWidget::SelfInfoWidget(QWidget *parent)
phoneEdit = new QLineEdit();
phoneEdit->setFixedHeight(height);
phoneEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
phoneEdit->setStyleSheet(btnStyle);
phoneEdit->setStyleSheet(editStyle);
//phoneEdit->setStyleSheet("QLineEdit {background-color: rgb(245, 245, 245); }");
phoneEdit->hide();
phoneSubmitBtn = new QPushButton();
@ -196,8 +193,34 @@ SelfInfoWidget::SelfInfoWidget(QWidget *parent)
nameLabel->setText("xyz");
descLabel->setText("It didn't matter that i lived another day.");
phoneLabel->setText("12345678900");
//此处只做测试,不要让界面显示默认头像
avatarBtn->setIcon(QIcon(":/resource/image/defaultAvatar.png"));
#endif
initSingnalSlots();
//加载数据到界面上
DataCenter* dataCenter = DataCenter::getInstance();
UserInfo* myself = dataCenter->getMyselfsync();
if (myself != nullptr) {
//把个人信息加载到界面上
avatarBtn->setIcon(myself->avatar);
idLabel->setText(myself->userId);
nameLabel->setText(myself->nickname);
descLabel->setText(myself->description);
phoneLabel->setText(myself->phone);
}
/*connect(nameSubmitBtn, &QPushButton::clicked, this, &SelfInfoWidget::clickNameSubmitBtn);
connect(descSubmitBtn, &QPushButton::clicked, this, &SelfInfoWidget::clickDescSubmitBtn);
connect(getVerifyCodeBtn, &QPushButton::clicked, this, &SelfInfoWidget::clickGetVerifyCodeBtn);
connect(phoneSubmitBtn, &QPushButton::clicked, this, &SelfInfoWidget::clickPhoneSubmitBtn);
connect(avatarBtn, &QPushButton::clicked, this, &SelfInfoWidget::clickAvatarBtn);*/
}
void SelfInfoWidget::initSingnalSlots()
{
//添加连接的槽函数
connect(nameModifyBtn, &QPushButton::clicked, this, [=]() {
//把当前的nameLabel和nameModifyBtn隐藏起来
@ -249,10 +272,209 @@ SelfInfoWidget::SelfInfoWidget(QWidget *parent)
phoneEdit->setText(phoneLabel->text());
});
/*connect(nameSubmitBtn, &QPushButton::clicked, this, &SelfInfoWidget::clickNameSubmitBtn);
connect(nameSubmitBtn, &QPushButton::clicked, this, &SelfInfoWidget::clickNameSubmitBtn);
connect(descSubmitBtn, &QPushButton::clicked, this, &SelfInfoWidget::clickDescSubmitBtn);
connect(getVerifyCodeBtn, &QPushButton::clicked, this, &SelfInfoWidget::clickGetVerifyCodeBtn);
connect(phoneSubmitBtn, &QPushButton::clicked, this, &SelfInfoWidget::clickPhoneSubmitBtn);
connect(avatarBtn, &QPushButton::clicked, this, &SelfInfoWidget::clickAvatarBtn);*/
connect(avatarBtn, &QPushButton::clicked, this, &SelfInfoWidget::clickAvatarBtn);
}
void SelfInfoWidget::clickNameSubmitBtn()
{
//从输入框中拿到修改后的呢称
const QString& nickName = nameEdit->text();
if (nickName.isEmpty()) {
return;
}
//发送网络请求
DataCenter* dataCenter = DataCenter::getInstance();
connect(dataCenter, &DataCenter::changeNicknameDone, this, &SelfInfoWidget::clickNameSubmitBtnDone, Qt::UniqueConnection);
dataCenter->changeNicknameAsync(nickName);
}
void SelfInfoWidget::clickNameSubmitBtnDone()
{
//对界面控件进行切换, 把输入框切换为label 把提交按钮切换为编辑按钮
//同时要把输入框的内容设置为修改后的呢称
layout->removeWidget(nameEdit);
nameEdit->hide();
layout->addWidget(nameLabel, 1, 2);
nameLabel->show();
nameLabel->setText(nameEdit->text());
layout->removeWidget(nameSubmitBtn);
nameSubmitBtn->hide();
layout->addWidget(nameModifyBtn, 1, 3);
nameModifyBtn->show();
}
void SelfInfoWidget::clickDescSubmitBtn()
{
//从输入框拿到输入的签名内容
const QString& desc = descEdit->text();
if (desc.isEmpty()) {
return;
}
//发送网络请求
DataCenter* dataCenter = DataCenter::getInstance();
connect(dataCenter, &DataCenter::changeDescriptionDone, this, &SelfInfoWidget::clickDescSubmitBtnDone, Qt::UniqueConnection);
dataCenter->changeDescriptionAsync(desc);
}
void SelfInfoWidget::clickDescSubmitBtnDone()
{
//切换界面
//把label替换为输入框把提交按钮替换为编辑按钮
layout->removeWidget(descEdit);
descEdit->hide();
layout->addWidget(descLabel, 2, 2);
descLabel->show();
descLabel->setText(descEdit->text());
layout->removeWidget(descSubmitBtn);
descSubmitBtn->hide();
layout->addWidget(descModifyBtn, 2, 3);
descModifyBtn->show();
}
void SelfInfoWidget::clickGetVerifyCodeBtn()
{
//获取到输入框的邮箱号
const QString& email = phoneEdit->text();
if (email.isEmpty()) {
QTimer* timer = new QTimer(this);
int* leftTime = new int(2);
phoneEdit->setPlaceholderText("请输入邮箱");
phoneEdit->setStyleSheet("QLineEdit { border: 1px solid red; background-color: rgb(255, 220, 220); color: rgb(0, 0, 0); border-radius: 5px; padding-left: 2px; }");
connect(timer, &QTimer::timeout, this, [=]() {
if (*leftTime <= 0) {
phoneEdit->setPlaceholderText("");
phoneEdit->setStyleSheet("QLineEdit { border: none; background-color: rgb(250 ,250, 250); color: rgb(0, 0, 0); border-radius: 5px; padding-left: 2px; }");
timer->stop();
timer->deleteLater();
delete timer;
delete leftTime;
return;
}
(*leftTime)--;
});
timer->start(1000); // 1秒后清除提示
return;
}
//给服务器发送请求
DataCenter* dataCenter = DataCenter::getInstance();
connect(dataCenter, &DataCenter::getVerifyCodeDone, this, [=]() {
//不需要做其他的处理,只需要提示一下,验证码已经发送出去了
Toast::showMessage("验证码已发送,请注意查收");
});
dataCenter->getVerifyCodeAsync(email);
//把刚才发送请求的邮箱号码保存起来
this->emailToChange = email;
//禁用发送验证码的按钮,给出倒计时
this->getVerifyCodeBtn->setEnabled(false);
leftTime = 30; // 倒计时30秒
QTimer* timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, [=]() {
if (leftTime <= 0) {
//倒计时结束
getVerifyCodeBtn->setEnabled(true);
getVerifyCodeBtn->setText("获取验证码");
timer->stop();
timer->deleteLater();
return;
}
(leftTime)--;
getVerifyCodeBtn->setText(QString::number(leftTime) + "s");
});
timer->start(1000); // 每秒更新一次
}
void SelfInfoWidget::clickPhoneSubmitBtn()
{
//判定,当前的验证码是否已经收到
DataCenter* dataCenter = DataCenter::getInstance();
QString verifyCodeId = dataCenter->getVerifyCodeId();
if (verifyCodeId.isEmpty()) {
//服务器还没有返回验证码响应
Toast::showMessage("服务器未返回响应,请稍后重试");
return;
}
//如果当前已经拿到了verifyCodeId就可以清空DataCenter中存储的值了确保下次点击提交按钮的时候不会影响当次的逻辑
dataCenter->resetVerifyCodeId("");
//获取到用户输入的验证码
QString verifyCode = verifyCodeEdit->text();
if (verifyCode.isEmpty()) {
Toast::showMessage("验证码不能为空");
return;
}
verifyCodeEdit->setText("");
//发送请求,把当前验证码信息,发送给服务器
connect(dataCenter, &DataCenter::changePhoneDone, this, &SelfInfoWidget::clickPhoneSubmitBtnDone, Qt::UniqueConnection);
dataCenter->changePhoneAsync(this->emailToChange, verifyCodeId, verifyCode);
leftTime = 1;
}
void SelfInfoWidget::clickPhoneSubmitBtnDone()
{
layout->removeWidget(verifyCodeTag);
layout->removeWidget(verifyCodeEdit);
layout->removeWidget(getVerifyCodeBtn);
layout->removeWidget(phoneEdit);
layout->removeWidget(phoneSubmitBtn);
verifyCodeTag->hide();
verifyCodeEdit->hide();
getVerifyCodeBtn->hide();
phoneEdit->hide();
phoneSubmitBtn->hide();
layout->addWidget(phoneLabel, 3, 2);
phoneLabel->show();
layout->addWidget(phoneModifyBtn, 3, 3);
phoneModifyBtn->show();
phoneLabel->setText(this->emailToChange);
}
void SelfInfoWidget::clickAvatarBtn()
{
//弹出对话框,选择文件
QString filter = "Image Files (*.png *.jpg *.jpeg)";
QString imagePath = QFileDialog::getOpenFileName(this, "选择头像", QDir::homePath(), filter);
if (imagePath.isEmpty()) {
//用户取消了
LOG() << "用户没有选择任何头像文件";
return;
}
//根据路径,读取到图片的内容
QByteArray imageBytes = loadFileToByteArray(imagePath);
//发送请求,修改头像
DataCenter* dataCenter = DataCenter::getInstance();
connect(dataCenter, &DataCenter::changeAvatarDone, this, &SelfInfoWidget::clickAvatarBtnDone, Qt::UniqueConnection);
dataCenter->changeAvatarAsync(imageBytes);
}
void SelfInfoWidget::clickAvatarBtnDone()
{
//设置头像,更新到界面上
DataCenter* dataCenter = DataCenter::getInstance();
avatarBtn->setIcon(dataCenter->getMyselfsync()->avatar);
}