QSqlTableModel類繼承至QSqlQueryModel類,該類提供了一個可讀寫單張SQL表的可編輯數(shù)據(jù)模型,功能:修改,插入,刪除,查詢,和排序
常用函數(shù)
QVariant headerData ( int section,Qt::Orientation orientation, int role = Qt::DisplayRole ) const 獲取水平頭或垂直頭標題
bool setHeaderData ( int section,Qt::Orientation orientation, const QVariant & value, int role = Qt::EditRole ) 設(shè)置水平頭或垂直頭標題
int rowCount ( const QModelIndex & parent= QModelIndex() ) const // 返回行數(shù)
int columnCount ( const QModelIndex &index = QModelIndex() ) const // 返回列數(shù)
virtual bool removeColumns ( int column, int count, const QModelIndex & parent = QModelIndex() ) //model->removeColumns (0)刪除第一列
bool QSqlTableModel::submitAll (),//提交所有被修改的數(shù)據(jù),然后修改的數(shù)據(jù)被保存在數(shù)據(jù)庫中
void QSqlTableModel::revertAll () //撤銷所有的修改,如果數(shù)據(jù)庫已經(jīng)被提交了修改,就不能通過撤銷修改改回來了
virtual void revertRow ( int row ) //恢復(fù)指定行的改變
void QSqlTableModel::setFilter ( const QString & filter ) //篩選,按照字符串filter對數(shù)據(jù)庫進行篩選,相當于SQL中的WHERE語句
bool QSqlTableModel::select () //在篩選和排序的條件下,將數(shù)據(jù)庫中符合要求的在mode表格中顯示出來
void QSqlTableModel::setSort ( int column, Qt::SortOrder order ) //排序操作。按照列和Qt::SortOrder排序。Qt::SortOrder有升序和降序
bool insertRow ( int row, const QModelIndex & parent = QModelIndex() ) //插入行
bool insertColumn ( int column, constQModelIndex & parent = QModelIndex() ) // 插入列
model->setEditStrategy(QSqlTableModel::OnManualSubmit); //設(shè)置保存策略為手動提交
一、在QTableView中顯示數(shù)據(jù)庫中表的數(shù)據(jù)
- QSqlTableModel *model = new QSqlTableModel(parentObject, database);
- model->setTable("employee");
- model->setEditStrategy(QSqlTableModel::OnManualSubmit);
- model->select();
- model->removeColumn(0);
- model->setHeaderData(0, Qt::Horizontal, tr("Name"));
- model->setHeaderData(1, Qt::Horizontal, tr("Salary"));
-
- QTableView *view = new QTableView;
- view->setModel(model);
- view->show();
二、修改QTableView中數(shù)據(jù)后的提交,加入事務(wù)處理
- model->database().transaction();
- if (model->submitAll())
- {
- model->database().commit();
- } else {
- model->database().rollback();
- QMessageBox::warning(this, tr(“tableModel”),tr(“數(shù)據(jù)庫錯誤: %1″).arg(model->lastError().text()));
- }
- model->revertAll();
三、查詢操作
相當于SQL語句:SELECT * FROM 表名 WHERE name = "name變量"
- model->setFilter(QObject::tr(“name = ‘%1′”).arg(name));
- model->select();
- for (int i = 0; i < model.rowCount(); ++i)
- {
- QString name = model.record(i).value("name").toString();
-
- }
-
-
- int primaryKeyIndex = model.record().indexOf("id");
- for (int i = 0; i < model.rowCount(); ++i)
- {
- QSqlRecord record = model.record(i);
- QString name = record.value("name").toString();
-
- }
四、排序操作
- model->setSort(0,Qt::AscendingOrder);
- model->select();
五、插入操作
- int rowNum = model->rowCount();
- int id = 自定義;
- model->insertRow(rowNum);
- model->setData(model->index(rowNum,0),id);
- model->submitAll();
六、刪除一條記錄
首先要定位到待刪除的行上
- model.setFilter("id = 10");
- model.select();
- if (model.rowCount() == 1)
- {
- model.removeRows(0,1)
- model.submitAll();
- }
在QTableView中刪除選中的一行
- int curRow = tableView->currentIndex().row();
- model->removeRow(curRow);
posted on 2011-11-22 17:47
再生的雄鷹 閱讀(9303)
評論(0) 編輯 收藏 引用