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

目录
相关文章
|
2月前
|
SQL 数据库连接 数据库
C++ Qt开发:QSqlDatabase数据库组件
Qt SQL模块是Qt框架的一部分,它提供了一组类和函数,用于在Qt应用程序中进行数据库操作。这个模块的目标是简化数据库访问和操作,并提供一致的接口,使得开发者可以方便地与不同数据库系统进行交互。一般SQL组件常用的操作,包括读取数据、插入数据、更新数据、删除数据功能,这四个功能我将分别介绍它是如何使用的。Qt 是一个跨平台C++图形界面开发库,利用Qt可以快速开发跨平台窗体应用程序,在Qt中我们可以通过拖拽的方式将不同组件放到指定的位置,实现图形化开发极大的方便了开发效率,本章将重点介绍`QSqlDatabase`数据库模块的常用方法及灵活运用。
C++ Qt开发:QSqlDatabase数据库组件
|
2月前
|
Oracle Java 数据库连接
使用Mybatis generator自动生成代码,仅限Oracle数据库
使用Mybatis generator自动生成代码,仅限Oracle数据库
|
4月前
|
Linux
QT桌面(实现界面的滑动切换)
QT桌面(实现界面的滑动切换)
81 0
|
4月前
|
SQL Java 关系型数据库
Java的JDBC编程<-----用Java代码操作数据库(MySQL数据库)
Java的JDBC编程<-----用Java代码操作数据库(MySQL数据库)
20 0
|
1月前
|
编译器
QT creator开发环境下 界面更改后运行程序不能实时更新或者在源文件添加该控件后无法编译的问题
在使用QT Creator开发界面的过程中,偶尔会出现添加控件后,运行程序后,界面控件无法更新的情况,或者在源文件使用该控件却出现无法编译的情况,使用QT Creator 4.8.2也会出现这个情况,也不知道这种情况会不会在以后有所改善。
19 0
|
3天前
|
IDE 开发工具 C语言
QT案例IDE编写 -- 代码编写
QT案例IDE编写 -- 代码编写
5 0
|
4天前
|
SQL 安全 Devops
一个简单的代码拼写错误导致17个生产数据库被删!微软Azure DevOps宕机10小时始末
一个简单的代码拼写错误导致17个生产数据库被删!微软Azure DevOps宕机10小时始末
|
17天前
|
关系型数据库 MySQL 数据库
MySQL员工打卡日志表——数据库练习
MySQL员工打卡日志表——数据库练习
20 0
|
19天前
|
容器
QT基础入门——界面布局和常用控件(四)
QT基础入门——界面布局和常用控件(四)
17 0
QT基础入门——界面布局和常用控件(四)
|
1月前
|
存储 安全 数据库
电商管理系统的数据库设计思路和数据库代码
电商管理系统的数据库设计思路和数据库代码
39 0

推荐镜像

更多