解析 Qt 登陆窗口 查询数据库实例操作

移动开发
Qt 登陆窗口 查询数据库实例操作是本文要介绍的内容,实现一个与数据库连接的登陆窗口,我们先来看内容。

本文介绍的是Qt 登陆窗口 查询数据库实例操作的内容,主要是以代码的形式为友们介绍,我们先来看内容。

数据库:Sqlite3

数据库名:student

表名:student

表的结构:

 Qt 登陆窗口 查询数据库实例操作

使用的工具是:SQLite Database Browser

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

新建工程login

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

 Qt 登陆窗口 查询数据库实例操作

新建类loginDialog,继承自QDialog

  1. logindialog.h:   
  2.  
  3. #include <QDialog>     
  4. #include <QSqlTableModel>     
  5. class loginDialog : public QDialog     
  6. {     
  7.     Q_OBJECT     
  8. public:     
  9.     explicit loginDialog(QWidget *parent = 0);     
  10.     QString GetName();     
  11.     QString GetPwd();     
  12. signals:     
  13. public slots:     
  14.     void login_clicked();     
  15. private:     
  16.     QLabel *label_Name;     
  17.     QLabel *label_Pwd;     
  18.     QLineEdit *line_Name;     
  19.     QLineEdit *line_Pwd;     
  20.     QPushButton *btn_Login;     
  21.     QPushButton *btn_Cancle;     
  22.     QString name;     
  23.     QString pwd;     
  24. };     
  25. #endif // LOGINDIALOG_H    
  26. #include <QDialog> 
  27. #include <QSqlTableModel> 
  28. class loginDialog : public QDialog  
  29. {  
  30.     Q_OBJECT  
  31. public:  
  32.     explicit loginDialog(QWidget *parent = 0);  
  33.     QString GetName();  
  34.     QString GetPwd();  
  35. signals:  
  36. public slots:  
  37.     void login_clicked();  
  38. private:  
  39.     QLabel *label_Name;  
  40.     QLabel *label_Pwd;  
  41.     QLineEdit *line_Name;  
  42.     QLineEdit *line_Pwd;  
  43.     QPushButton *btn_Login;  
  44.     QPushButton *btn_Cancle;  
  45.     QString name;  
  46.     QString pwd;  
  47. };  
  48. #endif // LOGINDIALOG_H  

#p#

logindialog.cpp

  1. #include "logindialog.h"     
  2. loginDialog::loginDialog(QWidget *parent) :     
  3.     QDialog(parent)     
  4. {     
  5.     label_Name = new QLabel(tr("登录名:"));     
  6.     label_Pwd = new QLabel(tr("密 码:"));     
  7.     line_Name = new QLineEdit();     
  8.     line_Pwd = new QLineEdit();     
  9.     btn_Login = new QPushButton(tr("确认"));     
  10.     btn_Cancle = new QPushButton(tr("取消"));     
  11.     
  12.     line_Pwd->setEchoMode(QLineEdit::Password);     
  13.     label_Name->setMaximumWidth(40);     
  14.     label_Pwd->setMaximumWidth(40);     
  15.     line_Name->setMaximumWidth(100);     
  16.     line_Pwd->setMaximumWidth(100);     
  17.     QHBoxLayout *h1 = new QHBoxLayout();     
  18.     QHBoxLayout *h2 = new QHBoxLayout();     
  19.     QHBoxLayout *h3 = new QHBoxLayout();     
  20.     h1->addWidget(label_Name);     
  21.     h1->addWidget(line_Name);     
  22.     h2->addWidget(label_Pwd);     
  23.     h2->addWidget(line_Pwd);     
  24.     h3->addWidget(btn_Login);     
  25.     h3->addWidget(btn_Cancle);     
  26.     QVBoxLayout *v = new QVBoxLayout();     
  27.     v->addLayout(h1);     
  28.     v->addLayout(h2);     
  29.     v->addLayout(h3);     
  30.     this->setLayout(v);     
  31.     this->resize(200, 150);     
  32.     this->setMaximumSize(200, 150);     
  33.     connect(btn_Cancle, SIGNAL(clicked()), this, SLOT(close()));     
  34.     connect(btn_Login, SIGNAL(clicked()), this, SLOT(login_clicked()));     
  35. }     
  36. void loginDialog::login_clicked()     
  37. {     
  38.     name = line_Name->text();     
  39.     pwd = line_Pwd->text();     
  40.     QSqlTableModel model;     
  41.     model.setTable("student");     
  42.     model.setFilter(tr("id = '%1' and pwd = '%2'").arg(name).arg(pwd));     
  43.     model.select();     
  44.     if(model.rowCount()==1)//查询到有一个结果     
  45.     {     
  46.         accept();//隐含窗口,并返回结果QDialg::Accepted     
  47.     }else    
  48.     {     
  49.         QMessageBox::warning(this, tr("warn"), tr("用户名或者密码不正确"));     
  50.         line_Name->clear();     
  51.         line_Pwd->clear();     
  52.         line_Name->setFocus();     
  53.     }     
  54. }     
  55. //返回登陆名     
  56. QString loginDialog::GetName()     
  57. {     
  58.     return name;     
  59. }     
  60. //返回密码     
  61. QString loginDialog::GetPwd()     
  62. {     
  63.     return pwd;     
  64. }    
  65. #include "logindialog.h"  
  66. loginDialog::loginDialog(QWidget *parent) :  
  67.     QDialog(parent)  
  68. {  
  69.     label_Name = new QLabel(tr("登录名:"));  
  70.     label_Pwd = new QLabel(tr("密 码:"));  
  71.     line_Name = new QLineEdit();  
  72.     line_Pwd = new QLineEdit();  
  73.     btn_Login = new QPushButton(tr("确认"));  
  74.     btn_Cancle = new QPushButton(tr("取消"));  
  75.     line_Pwd->setEchoMode(QLineEdit::Password);  
  76.     label_Name->setMaximumWidth(40);  
  77.     label_Pwd->setMaximumWidth(40);  
  78.     line_Name->setMaximumWidth(100);  
  79.     line_Pwd->setMaximumWidth(100);  
  80.     QHBoxLayout *h1 = new QHBoxLayout();  
  81.     QHBoxLayout *h2 = new QHBoxLayout();  
  82.     QHBoxLayout *h3 = new QHBoxLayout();  
  83.     h1->addWidget(label_Name);  
  84.     h1->addWidget(line_Name);  
  85.     h2->addWidget(label_Pwd);  
  86.     h2->addWidget(line_Pwd);  
  87.     h3->addWidget(btn_Login);  
  88.     h3->addWidget(btn_Cancle);  
  89.     QVBoxLayout *v = new QVBoxLayout();  
  90.     v->addLayout(h1);  
  91.     v->addLayout(h2);  
  92.     v->addLayout(h3);  
  93.     this->setLayout(v);  
  94.     this->resize(200, 150);  
  95.     this->setMaximumSize(200, 150);  
  96.     connect(btn_Cancle, SIGNAL(clicked()), this, SLOT(close()));  
  97.     connect(btn_Login, SIGNAL(clicked()), this, SLOT(login_clicked()));  
  98. }  
  99. void loginDialog::login_clicked()  
  100. {  
  101.     name = line_Name->text();  
  102.     pwd = line_Pwd->text();  
  103.     QSqlTableModel model;  
  104.     model.setTable("student");  
  105.     model.setFilter(tr("id = '%1' and pwd = '%2'").arg(name).arg(pwd));  
  106.     model.select();  
  107.     if(model.rowCount()==1)//查询到有一个结果  
  108.     {  
  109.         accept();//隐含窗口,并返回结果QDialg::Accepted  
  110.     }else  
  111.     {  
  112.         QMessageBox::warning(this, tr("warn"), tr("用户名或者密码不正确"));  
  113.         line_Name->clear();  
  114.         line_Pwd->clear();  
  115.         line_Name->setFocus();  
  116.     }  
  117. }  
  118. //返回登陆名  
  119. QString loginDialog::GetName()  
  120. {  
  121.     return name;  
  122. }  
  123. //返回密码  
  124. QString loginDialog::GetPwd()  
  125. {  
  126.     return pwd;  

#p#

widget.h:

  1. view plaincopy to clipboardprint?  
  2. #ifndef WIDGET_H     
  3. #define WIDGET_H     
  4. #include <QWidget>     
  5. #include <QSqlTableModel>     
  6. namespace Ui {     
  7.     class Widget;     
  8. }     
  9. class Widget : public QWidget     
  10. {     
  11.     Q_OBJECT     
  12. public:     
  13.     explicit Widget(QString n, QString p, QWidget *parent = 0);     
  14.     ~Widget();     
  15. private:     
  16.     Ui::Widget *ui;     
  17.     QString name;     
  18.     QString pwd;     
  19.     QSqlTableModel *model;     
  20. };     
  21. #endif // WIDGET_H    
  22. #ifndef WIDGET_H  
  23. #define WIDGET_H  
  24. #include <QWidget> 
  25. #include <QSqlTableModel> 
  26. namespace Ui {  
  27.     class Widget;  
  28. }  
  29. class Widget : public QWidget  
  30. {  
  31.     Q_OBJECT  
  32. public:  
  33.     explicit Widget(QString n, QString p, QWidget *parent = 0);  
  34.     ~Widget();  
  35. private:  
  36.     Ui::Widget *ui;  
  37.     QString name;  
  38.     QString pwd;  
  39.     QSqlTableModel *model;  
  40. };  
  41. #endif // WIDGET_H 

widget.cpp

  1. view plaincopy to clipboardprint?  
  2. #include "widget.h"     
  3. #include "ui_widget.h"     
  4. Widget::Widget(QString n, QString p, QWidget *parent) :     
  5.     QWidget(parent),     
  6.     ui(new Ui::Widget)     
  7. {     
  8.     ui->setupUi(this);     
  9.     nname = n;     
  10.     ppwd = p;     
  11.     model = new QSqlTableModel(this);     
  12.     model->setTable("student");     
  13.     model->setFilter(tr("id = '%1'").arg(name));     
  14.     model->select();     
  15.     ui->label->setText(tr("%1,欢迎您! 您的信息如下:").arg(name));     
  16.     ui->tableView->setModel(model);     
  17.     ui->tableView->resizeColumnsToContents();     
  18.     ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);     
  19.     
  20. }     
  21.     
  22. Widget::~Widget()     
  23. {     
  24.     delete ui;     
  25. }    
  26. #include "widget.h"  
  27. #include "ui_widget.h"  
  28. Widget::Widget(QString n, QString p, QWidget *parent) :  
  29.     QWidget(parent),  
  30.     ui(new Ui::Widget)  
  31. {  
  32.     ui->setupUi(this);  
  33.     nname = n;  
  34.     ppwd = p;  
  35.     model = new QSqlTableModel(this);  
  36.     model->setTable("student");  
  37.     model->setFilter(tr("id = '%1'").arg(name));  
  38.     model->select();  
  39.     ui->label->setText(tr("%1,欢迎您! 您的信息如下:").arg(name));  
  40.     ui->tableView->setModel(model);  
  41.     ui->tableView->resizeColumnsToContents();  
  42.     ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);  
  43.  
  44. }  
  45. Widget::~Widget()  
  46. {  
  47.     delete ui;  

#p#

main.cpp

  1. view plaincopy to clipboardprint?  
  2. #include <QtGui/QApplication>     
  3. #include <QTextCodec>     
  4. #include <QSqlDatabase>     
  5. #include <QSqlError>     
  6. #include "widget.h"     
  7. #include "logindialog.h"     
  8. static bool createConnection()     
  9. {     
  10.     QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");     
  11.     db.setDatabaseName("student");     
  12.     if (!db.open()) {     
  13.         QMessageBox::critical(0, qApp->tr("Cannot open database"),     
  14.             qApp->tr("Unable to establish a database connection.\n"    
  15.                      "This example needs SQLite support. Please read "    
  16.                      "the Qt SQL driver documentation for information how "    
  17.                      "to build it.\n\n"    
  18.                      "Click Cancel to exit."), QMessageBox::Cancel);     
  19.         return false;     
  20.     }     
  21.     return true;     
  22. }     
  23. int main(int argc, char *argv[])     
  24. {     
  25.     QApplication a(argc, argv);     
  26.     QTextCodec::setCodecForTr(QTextCodec::codecForName("gb18030"));     
  27.     if(!createConnection())     
  28.     {     
  29.         return 1;     
  30.     }     
  31.     loginDialog l;     
  32.     QString Name;     
  33.     QString Pwd;     
  34.     if(l.exec()==QDialog::Accepted)     
  35.     {     
  36.         Name = l.GetName();     
  37.         Pwd = l.GetPwd();     
  38.         Widget w(Name, Pwd);     
  39.         w.show();     
  40.         return a.exec();     
  41.     }else    
  42.     {     
  43.         return 0;     
  44.     }     
  45. }    
  46. #include <QtGui/QApplication> 
  47. #include <QTextCodec> 
  48. #include <QSqlDatabase> 
  49. #include <QSqlError> 
  50. #include "widget.h"  
  51. #include "logindialog.h"  
  52. static bool createConnection()  
  53. {  
  54.     QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");  
  55.     db.setDatabaseName("student");  
  56.     if (!db.open()) {  
  57.         QMessageBox::critical(0, qApp->tr("Cannot open database"),  
  58.             qApp->tr("Unable to establish a database connection.\n"  
  59.                      "This example needs SQLite support. Please read "  
  60.                      "the Qt SQL driver documentation for information how "  
  61.                      "to build it.\n\n"  
  62.                      "Click Cancel to exit."), QMessageBox::Cancel);  
  63.         return false;  
  64.     }  
  65.     return true;  
  66. }  
  67. int main(int argc, char *argv[])  
  68. {  
  69.     QApplication a(argc, argv);  
  70.     QTextCodec::setCodecForTr(QTextCodec::codecForName("gb18030"));  
  71.     if(!createConnection())  
  72.     {  
  73.         return 1;  
  74.     }  
  75.     loginDialog l;  
  76.     QString Name;  
  77.     QString Pwd;  
  78.     if(l.exec()==QDialog::Accepted)  
  79.     {  
  80.         Name = l.GetName();  
  81.         Pwd = l.GetPwd();  
  82.         Widget w(Name, Pwd);  
  83.         w.show();  
  84.         return a.exec();  
  85.     }else  
  86.     {  
  87.         return 0;  
  88.     }  

运行结果:

登陆界面:

 Qt 登陆窗口 查询数据库实例操作

正确:

 Qt 登陆窗口 查询数据库实例操作

错误:

 Qt 登陆窗口 查询数据库实例操作

小结:关于 Qt 登陆窗口 查询数据库实例操作的内容介绍完,希望本文对你有所帮助!

责任编辑:zhaolei 来源: 互联网
相关推荐

2011-07-05 14:46:34

2011-06-27 13:49:43

Qt 数据库 QSqlQueryM

2011-06-27 13:32:21

Qt 数据库 QSqlQueryM

2011-07-05 18:04:45

QT Mysql

2011-07-05 10:16:16

Qt 数据库 SQLite

2011-07-05 18:11:13

Qt 数据库

2011-06-21 15:11:04

QT 数据库

2011-08-03 16:01:24

iPhone应用开发 自动登陆

2011-07-05 16:08:10

2011-08-30 14:25:06

QT数据库

2010-04-06 11:30:09

Oracle 数据库

2011-06-21 15:48:41

2011-06-21 15:58:20

Qt 数据库

2011-08-30 14:38:50

QT数据库

2011-08-08 11:23:59

腾达路由器

2011-07-01 13:42:24

QT 数据库

2011-06-27 12:56:28

2013-11-07 16:03:54

Ubuntu技巧

2009-08-19 16:30:55

C#操作Access数

2011-07-26 18:11:56

iPhone Sqlite 数据库
点赞
收藏

51CTO技术栈公众号