解析 Qt 登陸窗口 查詢數(shù)據(jù)庫實(shí)例操作
本文介紹的是Qt 登陸窗口 查詢數(shù)據(jù)庫實(shí)例操作的內(nèi)容,主要是以代碼的形式為友們介紹,我們先來看內(nèi)容。
數(shù)據(jù)庫:Sqlite3
數(shù)據(jù)庫名:student
表名:student
表的結(jié)構(gòu):

使用的工具是:SQLite Database Browser
注意:記得把創(chuàng)建的數(shù)據(jù)庫文件student放到對應(yīng)的目錄(QT4.7是在login-build-desktop下)
新建工程login
跳到ui界面,放置QLabel和QViewTable兩個(gè)組件

新建類loginDialog,繼承自QDialog
- logindialog.h:
 - #include <QDialog>
 - #include <QSqlTableModel>
 - 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
 - #include <QDialog>
 - #include <QSqlTableModel>
 - 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
 
#p#
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("確認(rèn)"));
 - 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)//查詢到有一個(gè)結(jié)果
 - {
 - accept();//隱含窗口,并返回結(jié)果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;
 - }
 - #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("確認(rèn)"));
 - 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)//查詢到有一個(gè)結(jié)果
 - {
 - accept();//隱含窗口,并返回結(jié)果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;
 - }
 
#p#
widget.h:
- view plaincopy to clipboardprint?
 - #ifndef WIDGET_H
 - #define WIDGET_H
 - #include <QWidget>
 - #include <QSqlTableModel>
 - 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
 - #ifndef WIDGET_H
 - #define WIDGET_H
 - #include <QWidget>
 - #include <QSqlTableModel>
 - 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
- view plaincopy to clipboardprint?
 - #include "widget.h"
 - #include "ui_widget.h"
 - Widget::Widget(QString n, QString p, QWidget *parent) :
 - QWidget(parent),
 - ui(new Ui::Widget)
 - {
 - ui->setupUi(this);
 - nname = n;
 - ppwd = 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;
 - }
 - #include "widget.h"
 - #include "ui_widget.h"
 - Widget::Widget(QString n, QString p, QWidget *parent) :
 - QWidget(parent),
 - ui(new Ui::Widget)
 - {
 - ui->setupUi(this);
 - nname = n;
 - ppwd = 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;
 - }
 
#p#
main.cpp
- view plaincopy to clipboardprint?
 - #include <QtGui/QApplication>
 - #include <QTextCodec>
 - #include <QSqlDatabase>
 - #include <QSqlError>
 - #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;
 - }
 - }
 - #include <QtGui/QApplication>
 - #include <QTextCodec>
 - #include <QSqlDatabase>
 - #include <QSqlError>
 - #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;
 - }
 - }
 
運(yùn)行結(jié)果:
登陸界面:

正確:

錯(cuò)誤:

小結(jié):關(guān)于 Qt 登陸窗口 查詢數(shù)據(jù)庫實(shí)例操作的內(nèi)容介紹完,希望本文對你有所幫助!















 
 
 
 
 
 
 