数据库:Sqlite3
数据库名:student
表名:student
表的结构:
使用的工具是:SQLite Database Browser
注意:记得把创建的数据库文件student放到对应的目录(QT4.7是在login-build-desktop下)
新建工程login
跳到ui界面,放置QLabel和QViewTable两个组件
新建类loginDialog,继承自QDialog
logindialog.h:
#include
#include
class loginDialog : public QDialog
{
Q_OBJECT
public:
explicit loginDialog(QWidget *parent = 0);
QString GetName();
QString GetPwd();
signals:
public slots:
void login_clicked();
private:
QLabel *label_Name;
QLabel *label_Pwd;
QLineEdit *line_Name;
QLineEdit *line_Pwd;
QPushButton *btn_Login;
QPushButton *btn_Cancle;
QString name;
QString pwd;
};
#endif // LOGINDIALOG_H
logindialog.cpp
#include "logindialog.h"
loginDialog::loginDialog(QWidget *parent) :
QDialog(parent)
{
label_Name = new QLabel(tr("登录名:"));
label_Pwd = new QLabel(tr("密 码:"));
line_Name = new QLineEdit();
line_Pwd = new QLineEdit();
btn_Login = new QPushButton(tr("确认"));
btn_Cancle = new QPushButton(tr("取消"));
line_Pwd->setEchoMode(QLineEdit::Password);
label_Name->setMaximumWidth(40);
label_Pwd->setMaximumWidth(40);
line_Name->setMaximumWidth(100);
line_Pwd->setMaximumWidth(100);
QHBoxLayout *h1 = new QHBoxLayout();
QHBoxLayout *h2 = new QHBoxLayout();
QHBoxLayout *h3 = new QHBoxLayout();
h1->addWidget(label_Name);
h1->addWidget(line_Name);
h2->addWidget(label_Pwd);
h2->addWidget(line_Pwd);
h3->addWidget(btn_Login);
h3->addWidget(btn_Cancle);
QVBoxLayout *v = new QVBoxLayout();
v->addLayout(h1);
v->addLayout(h2);
v->addLayout(h3);
this->setLayout(v);
this->resize(200, 150);
this->setMaximumSize(200, 150);
connect(btn_Cancle, SIGNAL(clicked()), this, SLOT(close()));
connect(btn_Login, SIGNAL(clicked()), this, SLOT(login_clicked()));
}
void loginDialog::login_clicked()
{
name = line_Name->text();
pwd = line_Pwd->text();
QSqlTableModel model;
model.setTable("student");
model.setFilter(tr("id = '%1' and pwd = '%2'").arg(name).arg(pwd));
model.select();
if(model.rowCount()==1)//查询到有一个结果
{
accept();//隐含窗口,并返回结果QDialg::Accepted
}else
{
QMessageBox::warning(this, tr("warn"), tr("用户名或者密码不正确"));
line_Name->clear();
line_Pwd->clear();
line_Name->setFocus();
}
}
//返回登陆名
QString loginDialog::GetName()
{
return name;
}
//返回密码
QString loginDialog::GetPwd()
{
return pwd;
}
widget.h:
#ifndef WIDGET_H
#define WIDGET_H
#include
#include
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QString n, QString p, QWidget *parent = 0);
~Widget();
private:
Ui::Widget *ui;
QString name;
QString pwd;
QSqlTableModel *model;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QString n, QString p, QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
name = n;
pwd = p;
model = new QSqlTableModel(this);
model->setTable("student");
model->setFilter(tr("id = '%1'").arg(name));
model->select();
ui->label->setText(tr("%1,欢迎您! 您的信息如下:").arg(name));
ui->tableView->setModel(model);
ui->tableView->resizeColumnsToContents();
ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
}
Widget::~Widget()
{
delete ui;
}
main.cpp
#include
#include
#include
#include
#include "widget.h"
#include "logindialog.h"
static bool createConnection()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("student");
if (!db.open()) {
QMessageBox::critical(0, qApp->tr("Cannot open database"),
qApp->tr("Unable to establish a database connection./n"
"This example needs SQLite support. Please read "
"the Qt SQL driver documentation for information how "
"to build it./n/n"
"Click Cancel to exit."), QMessageBox::Cancel);
return false;
}
return true;
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTextCodec::setCodecForTr(QTextCodec::codecForName("gb18030"));
if(!createConnection())
{
return 1;
}
loginDialog l;
QString Name;
QString Pwd;
if(l.exec()==QDialog::Accepted)
{
Name = l.GetName();
Pwd = l.GetPwd();
Widget w(Name, Pwd);
w.show();
return a.exec();
}else
{
return 0;
}
}
运行结果:
登陆界面:
正确:
错误: