基于qt和mysql的学生注册系统(二)

接下来是connection.h头文件,主要用于建立数据库连接:

p, li { white-space: pre-wrap; }

    p, li { white-space: pre-wrap; }    
   #ifndef CONNECTION_H
#define CONNECTION_H
#include <QMessageBox>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QtSql>
#include <QVector>
#include <QString>
#include <mainwindow.h>
static bool createConnection()
{
    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("localhost");
    db.setDatabaseName("Enroll");
    db.setUserName("root");
    db.setPassword("1");
    qDebug() <<"hell";
    if(!db.open())
    {
        QMessageBox::critical(0, qApp->tr("Cannot open database"),
            qApp->tr("Unable to establish a database connection."
                     ), QMessageBox::Cancel);
        return false;
    }
    // test the connection
    QSqlQuery query;
    query.exec("SELECT a FROM Student");
    qDebug() <<query.size();
    while(query.next())
    {
        QString id = query.value(0).toString();
        QString type = query.value(1).toString();
        QString data = query.value(2).toString();
        qDebug() << id << ", " << type << ", " << data <<endl;
         qDebug() <<query.size();
    }
}
#endif // CONNECTION_H
然后是login类:
我的做法是从对话框读取用户名和密码,用于登录数据库。
头文件:
    p, li { white-space: pre-wrap; }    
   #ifndef LOGIN_H
#define LOGIN_H
#include <QWidget>
#include <QLineEdit>
#include <QLabel>
#include <QDialogButtonBox>
#include <QGridLayout>
#include <QDialog>
#include <QMessageBox>
class login : public QDialog
{
    Q_OBJECT
public:
    explicit login(QWidget *parent = 0);
signals:
public slots:
    void accept();
private:
    QLineEdit *usrEdit;
    QLineEdit *pwdEdit;
    QLabel *usrLabel;
    QLabel *pwdLabel;
    QDialogButtonBox *buttonBox;
    QGridLayout *grid;
};
#endif // LOGIN_H
cpp文件:
   
    p, li { white-space: pre-wrap; }    
   #include "login.h"
login::login(QWidget *parent) :
    QDialog(parent)
{
    this->usrLabel=new QLabel(tr("UserName:"));
    this->pwdLabel=new QLabel(tr("PassWord:"));
    this->usrEdit=new QLineEdit;
    this->pwdEdit=new QLineEdit;
    pwdEdit->setEchoMode(QLineEdit::Password);
    this->buttonBox= new QDialogButtonBox(QDialogButtonBox::Ok
                                          | QDialogButtonBox::Cancel);
    connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
    connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
    grid=new QGridLayout;
    grid->addWidget(usrLabel,0,0);
    grid->addWidget(usrEdit,0,1);
    grid->addWidget(pwdLabel,1,0);
    grid->addWidget(pwdEdit,1,1);
    grid->addWidget(buttonBox,2,1);
    this->setWindowTitle("Login");
    resize(100,200);
    this->setLayout(grid);
}
void login::accept()
{
    if(usrEdit->text()=="root"&&pwdEdit->text()=="1")
    {
    QDialog::accept();
    }
    else
    {
        QMessageBox::warning(this,tr("Warning"),tr("Error"),QMessageBox::Yes);
        usrEdit->clear();
        pwdEdit->clear();
        usrEdit->setFocus();
    }
}
下篇文章贴关于实现sql语句的几个类。
   
 
 
  

 

你可能感兴趣的:(mysql,数据库,database,qt,login,Signal)