Qt中数据编程主要分为以下两点:
1、利用qt提供类 访问数据库或者成为简单的数据库编程
2、数据库编程中引入model/view编程模型

基于model/view数据库编程:

qt提供model类: QSqlTableModel类 QSqlQueryModel类
Qt提供view类: QTableView

需要掌握两个知识:
(1)model/view编程步骤
(2)qt数据库编程步骤

案例:通过界面view控件实现对数据库中的一张表(姓名、年龄、成绩)中记录进行操作
(1)放置一个view控件 QTableView,设置属性
(2)连接数据库
(3)实例化模型和数据库表建立关联
(4)设置模型表头
(5)对模型数据进行 增加 删除 修改
(6)提交或者撤销

工程的参考代码如下:
widget.h中内容:

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QSqlTableModel>
#include <QSqlDatabase>
#include <QItemSelectionModel>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
Q_OBJECT

public:
explicit Widget(QWidget *parent = 0);
~Widget();

private slots:
void on_btnSubmit_clicked();

void on_btnRevert_clicked();

void on_btnInsert_clicked();

void on_btnDelete_clicked();

private:
Ui::Widget *ui;
QSqlTableModel *theModel;
QItemSelectionModel *theSelection;
QSqlDatabase db;
};

#endif // WIDGET_H

widget.cpp文件内容:

#include "widget.h"
#include "ui_widget.h"
#include <QDebug>

Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
ui->tableView->setSelectionBehavior(QAbstractItemView::SelectItems);
ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);
ui->tableView->setAlternatingRowColors(true);

db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("/home/hua/students.db");
if(!db.open())
{
qDebug()<<"connect error";
return;
}

theModel = new QSqlTableModel(this,db);
theModel->setTable("stu");
theModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
theModel->select();
ui->tableView->setModel(theModel);

theModel->setHeaderData(0,Qt::Horizontal,"姓名");
theModel->setHeaderData(theModel->fieldIndex("age"),Qt::Horizontal,"年龄");
theModel->setHeaderData(2,Qt::Horizontal,"成绩");

theSelection = new QItemSelectionModel(theModel);
ui->tableView->setSelectionModel(theSelection);

}

Widget::~Widget()
{
delete ui;
}

void Widget::on_btnSubmit_clicked()
{
bool ret = theModel->submitAll();
if(!ret)
{
qDebug()<<"save error";
}
}

void Widget::on_btnRevert_clicked()
{
theModel->revertAll();
}

void Widget::on_btnInsert_clicked()
{
int row = ui->tableView->currentIndex().row();
theModel->insertRow(row);
QModelIndex index = theModel->index(row,0);
theSelection->clearSelection();
theSelection->setCurrentIndex(index,QItemSelectionModel::Select);

theModel->setData(theModel->index(row,1),20);

}

void Widget::on_btnDelete_clicked()
{
int row = ui->tableView->currentIndex().row();
theModel->removeRow(row);
}

main.cpp文件内容:

#include "widget.h"
#include <QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();

return a.exec();
}

QSqlQueryModel类:只读模型,查看数据库中,不能修改
开发中QSqlQueryModel类和QSqlQuery类一起配合进行数据库开发

QT基于model/view数据库编程2的更多相关文章

  1. Qt基于model/view数据库编程3

    QSqlQueryModel和QSqlQuery类: 工程开发过程中将这两个类合起来使用,用QSqlQueryModel查询展示数据库中的数据,用QSqlQuery类执行sql语言,实现对数据库的操作 ...

  2. Qt的Model/View Framework解析(数据是从真正的“肉(raw)”里取得,Model提供肉,所以读写文件、操作数据库、网络通讯等一系列与数据打交道的工作就在model中做了)

    最近在看Qt的Model/View Framework,在网上搜了搜,好像中文的除了几篇翻译没有什么有价值的文章.E文的除了Qt的官方介绍,其它文章也很少.看到一个老外在blog中写道Model/Vi ...

  3. PyQt学习随笔:Qt中Model/View中的Model Index

    Qt中Model/View中的Model Index是一个类,该类用于定位Model/View中数据模型中的数据. Model Index是从QAbstractItemModel派生的子类,用于在项视 ...

  4. PyQt学习随笔:Qt中Model/View中的怎么构造View匹配的Model

    老猿Python博文目录 老猿Python博客地址 在<PyQt学习随笔:Qt中Model/View相关的主要类及继承关系>介绍了Model/View架构的主要类,在实际使用时,view相 ...

  5. Qt基于tcp协议网络编程

    基于Qt网络编程: 基于tcp协议 c/s模式编程 所需要的类:QTcpServer QTcpSocket 利用qt基于tcp协议编写c/s模式程序: 两个类中的信号: QTcpServer : ne ...

  6. Qt - 基于HTTP的网络编程

    HTTP(超文本传输协议 Hyper Text Transfer Protocol) 基于TCP/IP通信协议,属于应用层协议. 使用情况: HTTP是无连接(无连接的含义是限制每次连接只处理一个请求 ...

  7. Qt - 基于TCP的网络编程

    TCP(传输控制协议 Transmission Control Protocol) 可靠.面向数据流.面向连接  的传输协议.(许多应用层协议都是以它为基础:HTTP.FTP) 使用情况: 相比UDP ...

  8. Qt - 基于UDP的网络编程

    UDP(用户数据报协议 User Data Protocol) 轻量级.不可靠.面向数据报.无连接  的传输层协议. 适用情况: 网络数据大多为短消息: 拥有大量客户端: 对数据安全无特殊要求: 网络 ...

  9. PyQt学习随笔:Qt中Model/View相关的主要类及继承关系

    View相关类类继承关系: Model相关类类继承关系:

随机推荐

  1. servlet中this.getServletContext(); this.getServletConfig().getServletContext(); 的区别

    WEB容器在启动时,它会为每个WEB应用程序都创建一个对应的ServletContext对象,它代表当前web应用.ServletConfig对象中维护了ServletContext对象的引用,开发人 ...

  2. My first python application

    ''' Authon:WSE_Gordon This application is use for the costomer to login the application. The Costome ...

  3. CSS设计模式之三权分立模式篇 ( 转)

    转自 海玉的博客 市面上我们常常会看到各种各样的设计模式书籍,Java设计模式.C#设计模式.Ruby设计模式等等.在众多的语言设计模式中我唯独找不到关于CSS设计模式的资料,即使在网上找到类似内容, ...

  4. 《ArcGIS Runtime SDK for Android开发笔记》

    开发笔记之基础教程 ArcGIS Runtime SDK for Android 各版本下载地址 <ArcGIS Runtime SDK for Android开发笔记>——(1).And ...

  5. 思维导图与Spring Batch

    最近在学画图,又在复习Spring Batch.不解释,直接上图. 第三章,用XMind画的. 第五章,用iMindMap画的.

  6. 修改CPAN安装源

    更新CPAN镜像源的方法,以CentOS 6.5为例. 存储CPAN设置信息的文件路径为: /usr/share/perl/CPAN/Config.pm 使用vi打开文件 vi /usr/share/ ...

  7. *Amazon problem: 234. Palindrome Linked List (reverse the linked list with n time)

    Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false ...

  8. OC property(声明)

    @interface Student : NSObject { int _age; int _no; float _height; } // 当编译器遇到@property时,会自动展开成getter ...

  9. hdu-1754 I Hate It---线段树模板题

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1754 题目大意: 求区间最大值+单点修改 解题思路: 直接套用模板即可 #include<bi ...

  10. POJ-1080 Human Gene Functions---类似LCS

    题目链接: https://cn.vjudge.net/problem/POJ-1080 题目大意: 给定两组序列,要你求出它们的最大相似度,每个字母与其他字母或自身和空格对应都有一个打分,求在这两个 ...