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

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,如需转载请自行联系原作者

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

相关文章
阿里云服务器怎么设置密码?怎么停机?怎么重启服务器?
如果在创建实例时没有设置密码,或者密码丢失,您可以在控制台上重新设置实例的登录密码。本文仅描述如何在 ECS 管理控制台上修改实例登录密码。
19391 0
阿里云ECS云服务器初始化设置教程方法
阿里云ECS云服务器初始化是指将云服务器系统恢复到最初状态的过程,阿里云的服务器初始化是通过更换系统盘来实现的,是免费的,阿里云百科网分享服务器初始化教程: 服务器初始化教程方法 本文的服务器初始化是指将ECS云服务器系统恢复到最初状态,服务器中的数据也会被清空,所以初始化之前一定要先备份好。
13736 0
阿里云服务器如何登录?阿里云服务器的三种登录方法
购买阿里云ECS云服务器后如何登录?场景不同,阿里云优惠总结大概有三种登录方式: 登录到ECS云服务器控制台 在ECS云服务器控制台用户可以更改密码、更换系.
24445 0
asp.net Oracle数据库左侧目录树及右侧数据绑定及分页
效果图如下:   当前位置:水利管理 >> ...
717 0
阿里云服务器如何登录?阿里云服务器的三种登录方法
购买阿里云ECS云服务器后如何登录?场景不同,云吞铺子总结大概有三种登录方式: 登录到ECS云服务器控制台 在ECS云服务器控制台用户可以更改密码、更换系统盘、创建快照、配置安全组等操作如何登录ECS云服务器控制台? 1、先登录到阿里云ECS服务器控制台 2、点击顶部的“控制台” 3、通过左侧栏,切换到“云服务器ECS”即可,如下图所示 通过ECS控制台的远程连接来登录到云服务器 阿里云ECS云服务器自带远程连接功能,使用该功能可以登录到云服务器,简单且方便,如下图:点击“远程连接”,第一次连接会自动生成6位数字密码,输入密码即可登录到云服务器上。
32657 0
阿里云ECS云服务器初始化设置教程方法
阿里云ECS云服务器初始化是指将云服务器系统恢复到最初状态的过程,阿里云的服务器初始化是通过更换系统盘来实现的,是免费的,阿里云百科网分享服务器初始化教程: 服务器初始化教程方法 本文的服务器初始化是指将ECS云服务器系统恢复到最初状态,服务器中的数据也会被清空,所以初始化之前一定要先备份好。
14420 0
分布式关系型数据库服务 DRDS 支持跨 Schema 联机查询及 DML 事务写入等多项能力发布
信息摘要: 分布式关系型数据库服务 DRDS 支持跨 Schema 联机查询及 DML 事务写入等多项能力发布适用客户: 数据库使用者 / 分布式数据库使用者 / 开发者 / 互联网企业 / 金融保险行业 / 新零售行业版本/规格功能: 一、新功能 1.
954 0
手把手教你更改CentOS下的mysql数据库存储目录的位置
@(linux)[Centos, mysql, datadir, centos] 前言 随着数据量逐渐增大,阿里云上原来的硬盘已经吃不消了,于是新买了一块硬盘挂载上去(具体如何挂载硬盘,请参考阿里云快速硬盘挂载教程!!!!!)。
1216 0
Docker的Mysql数据库:把数据存储在本地目录
Docker mysql 把数据存储在本地目录,很简单,只需要映射本地目录到容器即可 1、加上-v参数 $ docker run -d -e MYSQL_ROOT_PASSWORD=admin --name mysql -v /data/mysql/data:/var/lib/mysql -p 33...
1235 0
+关注
20381
文章
0
问答
文章排行榜
最热
最新
相关电子书
更多
OceanBase 入门到实战教程
立即下载
阿里云图数据库GDB,加速开启“图智”未来.ppt
立即下载
实时数仓Hologres技术实战一本通2.0版(下)
立即下载