开发者社区> 余二五> 正文

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();
        }
}









本文转自 chen138 51CTO博客,原文链接:http://blog.51cto.com/chenboqiang/328681,如需转载请自行联系原作者

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
数仓(Lambda/Kappa)架构
1. 数仓相关概念 2. 数据处理系统OLTP和OLAP 3. OLAP分类 4. 数仓分层(ODS, DWD,DWM,DWS, ADS) 5. 离线数仓架构 6. 实时数仓架构 Lambda和Kappa架构
9 0
autojs输入法
牙叔教程 简单易懂
8 0
ECS使用有感
我是一名即将步入社会的大学生,随着网络法等相关专业知识的学习愈发强烈。查询资料时,常常会浏览到制作精美的个人站,因此产生了建设自己个人站的设想,但是由于业余时间少之甚少,同时听闻购买域名与服务器的价格不菲,因此计划一直未能实现
5 0
使用有感
我是一名即将步入社会的大学生,随着网络法等相关专业知识的学习愈发强烈。查询资料时,常常会浏览到制作精美的个人站,因此产生了建设自己个人站的设想,但是由于业余时间少之甚少,同时听闻购买域名与服务器的价格不菲,因此计划一直未能实现
5 0
5G终端标识SUPI,SUCI及IMSI解析
IMSI,SUPI,SUCI均为UE终端标识,区别在于IMSI为LTE终端标识,SUPI为5G非加密终端标识,一般等同于IMSI,SUCI为5G加密终端标识,需要解密后才能得到SUPI。
3 0
阿里云ecs使用体验
整了台服务器部署项目上线
5 0
ECS使用体验
在阿里云领取了一个服务器,分享一下使用体验
4 0
+关注
20380
文章
0
问答
文章排行榜
最热
最新
相关电子书
更多
JS零基础入门教程(上册)
立即下载
性能优化方法论
立即下载
手把手学习日志服务SLS,云启实验室实战指南
立即下载