dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include <QPushButton>
#include <QLineEdit>
#include <QLabel>
#include <QMessageBox>
#include <QGridLayout>
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
public:
//登录button以及取消button
QPushButton* loginButton;
QPushButton* cancelButton;
//账号和密码label
QLabel* name;
QLabel* password;
//输入框
QLineEdit* nameLineEdit;
QLineEdit* passwordLineEdit;
//布局
QGridLayout* layout;
explicit Dialog(QWidget *parent = 0);
~Dialog();
private slots:
void login();
void cancel();
private:
Ui::Dialog *ui;
};
#endif // DIALOG_H
dialog.m
#include "dialog.h"
#include "ui_dialog.h"
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
//设置主窗口的标题
setWindowTitle(tr("QQ 登陆"));
//设置label标签
name = new QLabel;
name->setText(tr("账号:"));
password = new QLabel;
password->setText(tr("密码:"));
//添加输入lineEdit控件
nameLineEdit = new QLineEdit;
passwordLineEdit = new QLineEdit;
//设置密码输入框接受的内容为黑色的圆点
passwordLineEdit->setEchoMode(QLineEdit::Password);
//设置登陆和取消的按钮
loginButton = new QPushButton;
loginButton->setText(tr("登陆"));
cancelButton = new QPushButton;
cancelButton->setText(tr("取消"));
//窗口布局,添加用到的控件
layout = new QGridLayout(this);
layout->addWidget(name,0,0);
layout->addWidget(nameLineEdit,0,1);
layout->addWidget(password,1,0);
layout->addWidget(passwordLineEdit,1,1);
layout->addWidget(loginButton,2,0);
layout->addWidget(cancelButton,3,0);
//链接信号与槽
connect(loginButton,SIGNAL(clicked()),this,SLOT(login()));
connect(cancelButton,SIGNAL(clicked()),this,SLOT(cancel()));
}
//登陆按钮接收到信号之后做的处理
void Dialog::login()
{
//用户名前如果有空格按照正确的处理
// if(nameLineEdit->text().trimmed() == tr("123456") && passwordLineEdit->text() == tr("456789"))
//如果输入的账号和密码已经存在且都相同,就接受
if(nameLineEdit->text() == tr("123456") && passwordLineEdit->text() == tr("456789"))
accept();
else//显示一个警告框
{
QMessageBox::warning(this,tr("Waring"),tr("user and password waring"),QMessageBox::Yes);
//登陆不正确时,清空账户输入框和密码框,然后光标自动跳转到账户名输入框
nameLineEdit->clear();
passwordLineEdit->clear();
nameLineEdit->setFocus();
}
}
//取消操作处理
void Dialog::cancel()
{
exit(1);
}
Dialog::~Dialog()
{
delete ui;
}