Qt數(shù)據(jù)庫 QSqlQueryModel實例操作 下篇
本文介紹的是Qt數(shù)據(jù)庫 QSqlQueryModel實例操作 下篇,接著上篇文章繼續(xù)介紹,Qt數(shù)據(jù)庫 QSqlQueryModel實例操作 下篇 剛開始我們就講到,這個模型默認是只讀的,所以我們在窗口上并不能對表格中的內(nèi)容進行修改。但是我們可以創(chuàng)建自己的模型,然后按照我們自己的意愿來顯示數(shù)據(jù)和修改數(shù)據(jù)。要想使其可讀寫,需要自己的類繼承自QSqlQueryModel,并且重寫setData() 和 flags() 兩個函數(shù)。如果我們要改變數(shù)據(jù)的顯示,就要重寫data() 函數(shù)。
下面的例子中我們讓student表的id屬性列顯示紅色,name屬性列可編輯。
1.我們在工程中添加C++ Class,然后Class name設(shè)為MySqlQueryModel,Base Class設(shè)為QSqlQueryModel,如下:
2.我們將mysqlquerymodel.h中的內(nèi)容更改如下:
- class MySqlQueryModel : public QSqlQueryModel
- {
- public:
- MySqlQueryModel();
- //下面三個函數(shù)都是虛函數(shù),我們對其進行重載
- Qt::ItemFlags flags(const QModelIndex &index) const;
- bool setData(const QModelIndex &index, const QVariant &value, int role);
- QVariant data(const QModelIndex &item, int role=Qt::DisplayRole) const;
- //
- private:
- bool setName(int studentId, const QString &name);
- void refresh();
- };
然后將mysqlquerymodel.cpp文件更改如下:
- #include “mysqlquerymodel.h”
- #include <QSqlQuery>
- #include <QColor>
- MySqlQueryModel::MySqlQueryModel()
- {
- }
- Qt::ItemFlags MySqlQueryModel::flags(
- const QModelIndex &index) const //返回表格是否可更改的標志
- {
- Qt::ItemFlags flags = QSqlQueryModel::flags(index);
- if (index.column() == 1) //第二個屬性可更改
- flags |= Qt::ItemIsEditable;
- return flags;
- }
- bool MySqlQueryModel::setData(const QModelIndex &index, const QVariant &value, int /* role */)
- //添加數(shù)據(jù)
- {
- if (index.column() < 1 || index.column() > 2)
- return false;
- QModelIndex primaryKeyIndex = QSqlQueryModel::index(index.row(), 0);
- int id = data(primaryKeyIndex).toInt(); //獲取id號
- clear();
- bool ok;
- if (index.column() == 1) //第二個屬性可更改
- ok = setName(id, value.toString());
- refresh();
- return ok;
- }
- void MySqlQueryModel::refresh() //更新顯示
- {
- setQuery(“select * from student”);
- setHeaderData(0, Qt::Horizontal, QObject::tr(“id”));
- setHeaderData(1, Qt::Horizontal, QObject::tr(“name”));
- }
- bool MySqlQueryModel::setName(int studentId, const QString &name) //添加name屬性的值
- {
- QSqlQuery query;
- query.prepare(“update student set name = ? where id = ?”);
- query.addBindValue(name);
- query.addBindValue(studentId);
- return query.exec();
- }
- QVariant MySqlQueryModel::data(const QModelIndex &index, int role) const
- //更改數(shù)據(jù)顯示樣式
- {
- QVariant value = QSqlQueryModel::data(index, role);
- if (role == Qt::TextColorRole && index.column() == 0)
- return qVariantFromValue(QColor(Qt::red)); //***個屬性的字體顏色為紅色
- return value;
- }
在widget.cpp文件中添加頭文件:#include “mysqlquerymodel.h”
然后更改函數(shù)如下:
- void Widget::on_pushButton_clicked()
- {
- QSqlQueryModel *model = new QSqlQueryModel;
- model->setQuery(“select * from student”);
- model->setHeaderData(0, Qt::Horizontal, tr(“id”));
- model->setHeaderData(1, Qt::Horizontal, tr(“name”));
- QTableView *view = new QTableView;
- view->setModel(model);
- view->show();
- MySqlQueryModel *myModel = new MySqlQueryModel; //創(chuàng)建自己模型的對象
- myModel->setQuery(“select * from student”);
- myModel->setHeaderData(0, Qt::Horizontal, tr(“id”));
- myModel->setHeaderData(1, Qt::Horizontal, tr(“name”));
- QTableView *view1 = new QTableView;
- view1->setWindowTitle(“mySqlQueryModel”); //修改窗口標題
- view1->setModel(myModel);
- view1->show();
- }
運行效果如下:
可以看到我們要的效果已經(jīng)出來了。本文章原創(chuàng)于www.yafeilinux.com
小結(jié):Qt數(shù)據(jù)庫 QSqlQueryModel實例操作 下篇的內(nèi)容介紹完了,希望本文對你有所幫助,更多資料請參考編輯推薦!