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

目录
相关文章
|
8月前
|
关系型数据库 MySQL 数据库
Qt实现登陆界面(含代码)
Qt实现登陆界面(含代码)
152 0
Qt实现登陆界面(含代码)
|
9月前
|
SQL 数据库 C++
C/C++ Qt 数据库QSql增删改查组件应用
Qt SQL模块是Qt中用来操作数据库的类,该类封装了各种SQL数据库接口,可以很方便的链接并使用,数据的获取也使用了典型的Model/View结构,通过MV结构映射我们可以实现数据与通用组件的灵活绑定,一般SQL组件常用的操作,包括,读取数据,插入数据,更新数据,删除数据,这四个功能我将分别介绍它是如何使用的。
243 0
C/C++ Qt 数据库QSql增删改查组件应用
|
9月前
|
SQL 数据库 C++
C/C++ Qt 数据库与TreeView组件绑定
在上一篇博文`《C/C++ Qt 数据库QSql增删改查组件应用》`介绍了Qt中如何使用SQL操作函数,并实现了对数据库的增删改查等基本功能,从本篇开始将实现数据库与View组件的绑定,通过数据库与组件关联可实现动态展示数据库中的表记录。
198 0
C/C++ Qt 数据库与TreeView组件绑定
|
9月前
|
存储 SQL 数据库
C/C++ Qt 数据库与ComBox多级联动
Qt中的SQL数据库组件可以与`ComBox`组件形成多级联动效果,在日常开发中多级联动效果应用非常广泛,例如当我们选择指定用户时,我们让其在另一个`ComBox`组件中列举出该用户所维护的主机列表,又或者当用户选择省份时,自动列举出该省份下面的城市列表等。
246 0
C/C++ Qt 数据库与ComBox多级联动
|
9月前
|
存储 数据库 C++
C/C++ Qt 数据库与TableView多组件联动
Qt 数据库组件与TableView组件实现联动,以下案例中实现了,当用户点击并选中TableView组件内的某一行时,我们通过该行中的name字段查询并将查询结果关联到`ListView`组件内,同时将TableView中选中行的字段分别显示在窗体底部的`LineEdit`编辑内,该案例具体实现细节如下。
240 0
C/C++ Qt 数据库与TableView多组件联动
|
9月前
|
SQL 数据库 C++
C/C++ Qt 数据库与SqlTableModel组件应用
SqlTableModel 组件可以将数据库中的特定字段动态显示在`TableView`表格组件中,通常设置`QSqlTableModel`类的变量作为数据模型后就可以显示数据表内容,界面组件中则通过`QDataWidgetMapper`类实例设置为与某个数据库字段相关联,则可以实现自动显示字段的内容,不仅是显示,其还支持`动态增删改查`等各种复杂操作,期间不需要使用任何SQL语句。
269 0
C/C++ Qt 数据库与SqlTableModel组件应用
|
9月前
|
存储 数据库 C++
C/C++ Qt 数据库SqlRelationalTable关联表
在上一篇博文中详细介绍了`SqlTableModle`组件是如何使用的,本篇博文将介绍`SqlRelationalTable`关联表组件,该组件其实是`SqlTableModle`组件的扩展类,`SqlRelationalTable`组件可以关联某个主表中的外键,例如将主表中的某个字段与附加表中的特定字段相关联起来,`QSqlRelation(关联表名,关联ID,名称)`就是用来实现多表之间快速关联的。
189 5
C/C++ Qt 数据库SqlRelationalTable关联表
|
10月前
|
存储 SQL 数据库
C/C++ Qt 数据库与Chart实现历史数据展示
在前面的博文中具体介绍了QChart组件是如何绘制各种通用的二维图形的,本章内容将继续延申一个新的知识点,通过数据库存储某一段时间节点数据的走向,当用户通过编辑框提交查询记录时,程序自动过滤出该时间节点下所有的数据,并将该数据动态绘制到图形组件内,实现动态查询图形的功能。
223 0
C/C++ Qt 数据库与Chart实现历史数据展示
|
存储 监控 BI
案例分享:Qt+RV1126+PLC医疗血浆采集仪(中英文输入、西门子PLC、数据库存储,各种数据统计,数值监测,电子秤操作,记录查询,全局报警等等)
案例分享:Qt+RV1126+PLC医疗血浆采集仪(中英文输入、西门子PLC、数据库存储,各种数据统计,数值监测,电子秤操作,记录查询,全局报警等等)
案例分享:Qt+RV1126+PLC医疗血浆采集仪(中英文输入、西门子PLC、数据库存储,各种数据统计,数值监测,电子秤操作,记录查询,全局报警等等)
|
SQL 数据库连接 数据库
Qt实用技巧:Qt连接SQL Server数据库(需要配置ODBC)
Qt实用技巧:Qt连接SQL Server数据库(需要配置ODBC)
推荐文章
更多
推荐镜像
更多