版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Kuangqiu/article/details/72871342
要求:一个简单的登录页面,模仿QQ登录,然后点击按钮进行数据的注册,注册成功后返回登录页面有用户名和密码的信息。
运行结果如图所示:
下面是我写的代码
第一个是登录logindialog.h#ifndef LOGINDIALOG_H
#define LOGINDIALOG_H
#include <QDialog>
namespace Ui {
class LoginDialog;
}
class LoginDialog : public QDialog
{
Q_OBJECT
public:
explicit LoginDialog(QWidget *parent = 0);
//explicit LoginDialog(QString username,QString password,QWidget *parent = 0);
~LoginDialog();
private slots:
void on_zcpushButton_clicked();
void getValue(QString user,QString paswd);
private:
Ui::LoginDialog *ui;
};
#endif // LOGINDIALOG_H
第二个是注册zcdialog.h
#ifndef LOGINDIALOG_H
#define LOGINDIALOG_H
#include <QDialog>
namespace Ui {
class LoginDialog;
}
class LoginDialog : public QDialog
{
Q_OBJECT
public:
explicit LoginDialog(QWidget *parent = 0);
//explicit LoginDialog(QString username,QString password,QWidget *parent = 0);
~LoginDialog();
private slots:
void on_zcpushButton_clicked();
void getValue(QString user,QString paswd);
private:
Ui::LoginDialog *ui;
};
#endif // LOGINDIALOG_H
第三个是logindialog.cpp
#include "logindialog.h"
#include "ui_logindialog.h"
#include<zcdialog.h>
LoginDialog::LoginDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::LoginDialog)
{
ui->setupUi(this);
}
/*
LoginDialog::LoginDialog(QString username, QString password, QWidget *parent):
QDialog(parent),
ui(new Ui::LoginDialog)
{
ui->setupUi(this);
ui->userlineEdit->setText(username);
ui->pwdlineEdit->setText(password);
}
*/
LoginDialog::~LoginDialog()
{
delete ui;
}
void LoginDialog::on_zcpushButton_clicked()
{
ZcDialog *pdlg = new ZcDialog;
connect(pdlg, SIGNAL(retvalue(QString,QString)),this, SLOT(getValue(QString,QString)));
pdlg->setAttribute(Qt::WA_DeleteOnClose);
pdlg->show();
}
void LoginDialog::getValue(QString user, QString paswd)
{
ui->userlineEdit->setText(user);
ui->pwdlineEdit->setText(paswd);
}
第四个是main.cpp
#include "logindialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
LoginDialog w;
w.show();
w.setWindowTitle("QQ登录窗口");
return a.exec();
}
第五个是zcdialog.cpp
#include "zcdialog.h"
#include "ui_zcdialog.h"
#include<QMessageBox>
#include<logindialog.h>
#include<QTimer>
#include<windows.h>
#include<QProgressBar>
#include<QProgressDialog>
ZcDialog::ZcDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::ZcDialog)
{
ui->setupUi(this);
//暗文生成
ui->pwdlineEdit->setEchoMode(QLineEdit::Password);
//组默认选择
ui->boyradioButton->setChecked(true);
}
ZcDialog::~ZcDialog()
{
delete ui;
}
void ZcDialog::on_zc2pushButton_clicked()
{
if(ui->userlineEdit->text().trimmed()!=""&&ui->pwdlineEdit->text().trimmed()!="")
{
QProgressDialog dialog(tr("注册进行中"),tr("取消"),0,99,this);
dialog.setWindowTitle(tr("进度条对话框"));
dialog.setWindowModality(Qt::WindowModal);
dialog.show();
for(int i=0;i<=99;i++)
{
dialog.setValue(i);
Sleep(50);
//QCoreApplication::processEvents();//防止界面冻结
if(dialog.wasCanceled()) return; //取消就中断
if(i==99)
{
QMessageBox::information(this,tr("系统提示"),tr("注册完毕"),QMessageBox::Ok);
}
}
//QMessageBox::information(this,tr("提示"),tr("注册成功"),QMessageBox::Ok|QMessageBox::Default);
#if 0
//模态
QString username=ui->userlineEdit->text();
QString pwd=ui->pwdlineEdit->text();
LoginDialog log(username,pwd);
log.exec();
#endif
//非模态窗口
QString username=ui->userlineEdit->text();
QString pwd=ui->pwdlineEdit->text();
emit retvalue(username,pwd);
this->close();
}
else
{
QMessageBox::warning(this,"warning","用户名或者密码不能为空",QMessageBox::Yes);
}
}
void ZcDialog::on_cancelpushButton_clicked()
{
this->close();
}
最后完工!