qt对mysql的查询窗口_Qt登陆窗口(查询数据库) | 学步园

数据库:Sqlite3

数据库名:student

表名:student

表的结构:

0_13059682552FoC.gif

使用的工具是:SQLite Database Browser

注意:记得把创建的数据库文件student放到对应的目录(QT4.7是在login-build-desktop下)

新建工程login

跳到ui界面,放置QLabel和QViewTable两个组件

0_1305967978YdY8.gif

新建类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;

}

}

运行结果:

登陆界面:

0_1305967954gtWr.gif

正确:

0_1305967966MqKI.gif

错误:

0_1305967936e792.gif

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值