此部分比较简单,包含两个dialog。
登陆界面:login.cpp
界面显示部分:
username_l = new QLabel(tr("用户名"),this);
username_l->move(70,80);
username_e = new QLineEdit(this);
username_e->move(140,80);
username_e->setPlaceholderText(tr("请输入用户名"));
password_l = new QLabel(tr("password"),this);
password_l->move(70,130);
password_e = new QLineEdit(this);
password_e->move(140,130);
password_e->setPlaceholderText(tr("请输入密码"));
log_btn = new QPushButton(tr("登陆"),this);
log_btn->move(50,200);
reg_btn = new QPushButton(tr("注册"),this);
reg_btn->move(210,200);
nam = new QNetworkAccessManager(this);
connect(log_btn,SIGNAL(clicked()),this,SLOT(loginTo()));
connect(reg_btn,SIGNAL(clicked()),this,SLOT(registerTo()));
connect(nam,SIGNAL(finished(QNetworkReply*)),this,SLOT(finishedSlot(QNetworkReply*)));
void Login::loginTo()
{
if(username_e->text().isEmpty() || password_e->text().isEmpty()) {
QMessageBox::warning(this,tr("提示"),tr("用户名和密码不能为空!"),QMessageBox::Yes);
return;
}
QUrl url("http://119.29.36.247:8080/campus/webapi/users/log_info");
QJsonObject log_info;
log_info.insert("username",username_e->text());
log_info.insert("password",password_e->text());
QNetworkRequest request;
request.setUrl(url);
request.setHeader(QNetworkRequest::ContentTypeHeader,"application/json");
nam->post(request,QJsonDocument(log_info).toJson());
}
void Login::finishedSlot(QNetworkReply *reply) {
QVariant statusCodeV = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
qDebug()<attribute(QNetworkRequest::RedirectionTargetAttribute);
if(reply->error() == QNetworkReply::NoError) {
QByteArray bytes = reply->readAll();
QString string = QString::fromUtf8(bytes);
qDebug()<deleteLater();
}
注册界面:register.cpp
界面显示部分:
username_l = new QLabel(tr("用户名"),this);
username_l->move(70,70);
username_e = new QLineEdit(this);
username_e->move(160,70);
username_e->setPlaceholderText(tr("请输入用户名"));
password_l = new QLabel(tr("密码"),this);
password_l->move(70,100);
password_e = new QLineEdit(this);
password_e->move(160,100);
password_e->setPlaceholderText("请输入密码");
password_e->setEchoMode(QLineEdit::Password);
repassword_l = new QLabel(tr("确认密码"),this);
repassword_l->move(70,130);
repassword_e = new QLineEdit(this);
repassword_e->move(160,130);
repassword_e->setPlaceholderText(tr("请再次输入密码"));
repassword_e->setEchoMode(QLineEdit::Password);
sex = new QButtonGroup(this);
sex_man = new QRadioButton(tr("男"),this);
sex_man->move(70,160);
sex_man->setChecked(true);
sex_woman = new QRadioButton(tr("女"),this);
sex_woman->move(150,160);
sex->addButton(sex_man,1);
sex->addButton(sex_woman,2);
submit = new QPushButton(tr("提交"),this);
submit->move(70,190);
cancel = new QPushButton(tr("取消"),this);
cancel->move(220,190);
nam = new QNetworkAccessManager(this);
connect(submit,SIGNAL(clicked(bool)),this,SLOT(reg_submit()));
connect(cancel,SIGNAL(clicked(bool)),this,SLOT(close()));
connect(nam,SIGNAL(finished(QNetworkReply*)),this,SLOT(get_reply(QNetworkReply*)));
void Register::reg_submit() {
if(username_e->text().isEmpty() || password_e->text().isEmpty() || repassword_e->text().isEmpty()) {
QMessageBox::warning(this,tr("提示"),tr("用户名/密码不能为空!"),QMessageBox::Yes);
}
else if(password_e->text() != repassword_e->text()) {
QMessageBox::warning(this,tr("提示"),tr("两次密码不匹配!"),QMessageBox::Yes);
}
else {
QUrl url("http://119.29.36.247:8080/campus/webapi/users/reg_info");
QJsonObject reg_info;
reg_info.insert("username",username_e->text());
reg_info.insert("password",password_e->text());
reg_info.insert("sex",sex->checkedId());
QNetworkRequest request;
request.setUrl(url);
request.setHeader(QNetworkRequest::ContentTypeHeader,"application/json");
nam->post(request,QJsonDocument(reg_info).toJson());
}
}
void Register::get_reply(QNetworkReply *reply) {
QVariant statusCodeV = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
qDebug()<attribute(QNetworkRequest::RedirectionTargetAttribute);
if(reply->error() == QNetworkReply::NoError) {
QByteArray bytes = reply->readAll();
QString string = QString::fromUtf8(bytes);
qDebug()<deleteLater();
}