Qt 数据库、登陆界面练习代码

*********main.cpp***********
 
#include <QApplication>
#include <QTextCodec>
#include "sqlite.h"
#include "database.h"
#include "login.h"
int main(int argc,char *argv[])
{
        QApplication app(argc,argv);
        QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK"));
        if(!createConnection())
                return 1;
        Sqlite sqlite;
        Login login;
        if(login.exec()==QDialog::Accepted)
        {
        sqlite.show();
        return app.exec();
        }
        else return 0;
}
 
 
 
********sqlite.h*********
 
 
#ifndef SQLITE_H
#define SQLITE_H
#include <QDialog>
                                                                                
class QPushButton;
class QVBoxLayout;
class QTableView;
class QLabel;
class QLineEdit;
class QGridLayout;
class QSqlTableModel;
                                                                               
class Sqlite:public QDialog
{
                                                                                
        Q_OBJECT
                                                                               
        public:
        Sqlite(QWidget *parent = 0);
                                                                                
        private slots:
        void addButton_clicked();
        void delButton_clicked();
        void editButton_clicked();
        void noEditButton_clicked();
        void ascendingButton_clicked();
        void descendingButton_clicked();
        void findButton_clicked();
        void returnTableButton_clicked();
                                                                               
        private:
        QPushButton *addButton;
        QPushButton *delButton;
        QPushButton *editButton;
        QPushButton *noEditButton;
        QPushButton *ascendingButton;
        QPushButton *descendingButton;
        QVBoxLayout *verticalLayout;
        QTableView *tableView;
        QLabel *label;
        QLineEdit *lineEdit;
        QPushButton *findButton;
        QPushButton *returnTableButton;
        QGridLayout *gridLayout;
        QGridLayout *gridLayout2;
        QSqlTableModel *model;
};
                                                                                
#endif
 
 
*******sqlite.cpp*******
 
 
#include <QtGui>
#include <QSqlTableModel>
#include <QSqlError>
#include <QMessageBox>
#include "sqlite.h"
                                                                               
Sqlite::Sqlite(QWidget *parent)
        :QDialog(parent)
{
                                                                               
        addButton = new QPushButton(tr(" 添加记录 "));
        delButton = new QPushButton(tr(" 删除记录 "));
        editButton = new QPushButton(tr(" 修改记录 "));
        noEditButton = new QPushButton(tr(" 撤销修改 "));
        ascendingButton = new QPushButton(tr(" id 升序排列 "));
        descendingButton = new QPushButton(tr(" id 降序排列 "));
                                                                               
        connect(addButton,SIGNAL(clicked()),this,SLOT(addButton_clicked()));
        connect(delButton,SIGNAL(clicked()),this,SLOT(delButton_clicked()));
        connect(editButton,SIGNAL(clicked()),this,SLOT(editButton_clicked()));
        connect(noEditButton,SIGNAL(clicked()),this,SLOT(noEditButton_clicked()));
        connect(ascendingButton,SIGNAL(clicked()),this,SLOT(ascendingButton_clicked()));
        connect(descendingButton,SIGNAL(clicked()),this,SLOT(descendingButton_clicked()));
                                                                               
        verticalLayout = new QVBoxLayout;
        verticalLayout->addWidget(addButton);
        verticalLayout->addWidget(delButton);
        verticalLayout->addWidget(editButton);
        verticalLayout->addWidget(noEditButton);
        verticalLayout->addWidget(ascendingButton);
        verticalLayout->addWidget(descendingButton);
                                                                                
                                                                               
        label = new QLabel(tr(" 姓名 :"));
        lineEdit = new QLineEdit;
        label->setBuddy(lineEdit);
                                                                               
        tableView = new QTableView;
        model = new QSqlTableModel(this);
        model->setTable("student");
        model->setEditStrategy(QSqlTableModel::OnManualSubmit);
        model->select();
        tableView->setModel(model);
                                                                               
        findButton = new QPushButton(tr(" 查找 "));
        returnTableButton = new QPushButton(tr(" 返回全表 "));
                                                                               
        connect(findButton,SIGNAL(clicked()),this,SLOT(findButton_clicked()));
        connect(returnTableButton,SIGNAL(clicked()),this,SLOT(returnTableButton_clicked()));
                                                                               
        gridLayout = new QGridLayout;
        gridLayout->addWidget(tableView, 0, 0, 1, 2);
        gridLayout->addWidget(label, 1, 0, 1, 1);
        gridLayout->addWidget(lineEdit, 1, 1, 1, 1);
        gridLayout->addWidget(findButton, 2, 0, 1, 1);
        gridLayout->addWidget(returnTableButton, 2, 1, 1, 1);
                                                                                
                                                                               
        gridLayout2 = new QGridLayout;
        gridLayout2->addLayout(gridLayout, 0, 0, 1, 1);
        gridLayout2->addLayout(verticalLayout, 0, 1, 1, 1);
                                                                               
        setLayout(gridLayout2);
                                                                               
        setWindowTitle(tr("QSQLITE"));
}
                                                                                
void Sqlite::addButton_clicked()
{
        int rowNum = model->rowCount();
        int id = 10;
        model->insertRow(rowNum);
        model->setData(model->index(rowNum,0),id);
        model->submitAll();
                                                                               
}
                                                                               
void Sqlite::delButton_clicked()
{
        int curRow = tableView->currentIndex().row();
        model->removeRow(curRow);
        int ok = QMessageBox::warning(this,tr(" 删除 "),tr(" 确定删除当前行 ?"),QMessageBox::Yes,QMessageBox::No);
        if(ok == QMessageBox::No)
        {
                model->revertAll();
        }
        else model->submitAll();
                                                                               
}
                                                                               
void Sqlite::editButton_clicked()
{
        model->database().transaction();
        if(model->submitAll())
        {
                model->database().commit();
        }else{
                model->database().rollback();
                QMessageBox::warning(this,tr("tableModel"),tr("sjdfk:%1").arg(model->lastError().text()));
                                                                                
                                                                               
        }
                                                                               
}
                                                                                
void Sqlite::noEditButton_clicked()
{
                                                                               
         model->revertAll();
}
                                                                                
void Sqlite::ascendingButton_clicked()
{
        model->setSort(0,Qt::AscendingOrder);
        model->select();
                                                                               
}
                                                                                
void Sqlite::descendingButton_clicked()
{
        model->setSort(0,Qt::DescendingOrder);
        model->select();
}
                                                                                
void Sqlite::findButton_clicked()
{
        QString name = lineEdit->text();
        model->setFilter(QObject::tr("name = '%1'").arg(name));
        model->select();
}
                                                                                
void Sqlite::returnTableButton_clicked()
{
        model->setTable("student");
        model->select();
                                                                               
}
 
 
*********database.h**********
 
 
#ifndef DATABASE_H
#define DATABASE_H
                                                                               
                                                                               
#include <QSqlDatabase>
#include <QSqlQuery>
                                                                          
                                                                               
static bool createConnection()
{
        QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
        db.setDatabaseName("database.db");
        if(!db.open())
        return false;
        QSqlQuery query;
        query.exec(QObject::tr("create table student(id int primary key,name varchar)"));
        query.exec("insert into student values(1,'chen')");
        query.exec("insert into student values(2,'bo')");
        query.exec("insert into student values(3,'qiang')");
        return true;
}
                                                                               
                                                                                
#endif
 
 
**********login.h********
 
 
#ifndef LOGIN_H
#define LOGIN_H
#include <QDialog>
class QLabel;
class QLineEdit;
class QPushButton;
class QGridLayout;
class Login:public QDialog
{
        Q_OBJECT
                                                                               
        public:
        Login(QWidget *parent =0);
                                                                               
         private slots:
         void on_loginButton_clicked();
                                                                               
                                                                               
        private:
        QGridLayout *gridLayout;
        QGridLayout *gridLayout2;
        QLabel *LoginTopLabel;
        QLabel *userLabel;
        QLineEdit *userLineEdit;
        QLabel *pwdLabel;
        QLineEdit *pwdLineEdit;
        QPushButton *loginButton;
        QPushButton *exitButton;
                                                                                
                                                                               
};
 
#endif
 
 
**********login.cpp***********
 
#include <QtGui>
#include "login.h"
                                                                                
                                                                               
Login::Login(QWidget *parent)
        :QDialog(parent)
{
                                                                                
                                                                               
        LoginTopLabel = new QLabel(tr("<font color = red><h2><center> 用户登录界
"));
        LoginTopLabel->setFrameShape(QFrame::Panel);
        LoginTopLabel->setFrameShadow(QFrame::Sunken);
                                                                               
                                                                               
        userLabel = new QLabel(tr(" 用户姓名 :"));
        userLineEdit = new QLineEdit;
        userLabel->setBuddy(userLineEdit);
                                                                               
                                                                               
        pwdLabel = new QLabel(tr(" 用户密码 :"));
        pwdLineEdit = new QLineEdit;
        pwdLabel->setBuddy(pwdLineEdit);
        pwdLineEdit->setEchoMode(QLineEdit::Password);
                                                                               
        loginButton = new QPushButton(tr(" 登陆 "));
        exitButton = new QPushButton(tr(" 退出 "));
        connect(loginButton,SIGNAL(clicked()),this,SLOT(on_loginButton_clicked()));
        connect(exitButton,SIGNAL(clicked()),this,SLOT(close()));
 
gridLayout = new QGridLayout;
        gridLayout->addWidget(LoginTopLabel, 0, 0, 1, 2);
        gridLayout->addWidget(userLabel, 1, 0, 1, 1);
        gridLayout->addWidget(userLineEdit, 1, 1, 1, 1);
        gridLayout->addWidget(pwdLabel, 2, 0, 1, 1);
        gridLayout->addWidget(pwdLineEdit, 2, 1, 1, 1);
        gridLayout->addWidget(loginButton, 3, 0, 1, 1);
        gridLayout->addWidget(exitButton, 3, 1, 1, 1);
                                                                               
        gridLayout2 = new QGridLayout;
        gridLayout2->addLayout(gridLayout, 0, 0, 1, 1);
                                                                               
        setLayout(gridLayout2);
                                                                               
                                                                                
}
                                                                               
void Login::on_loginButton_clicked()
{
        if(userLineEdit->text().trimmed()==tr("admin")&&pwdLineEdit->text()==tr("123456"))
        accept();
        else{
                QMessageBox::warning(this,tr("Warning"),tr("user name or password error!"),QMessageBox::Yes);
                                                                               
                                                                                
        userLineEdit->clear();
        pwdLineEdit->clear();
        userLineEdit->setFocus();
        }
}

你可能感兴趣的:(数据库,代码,练习,登陆,界面)