62 lines
1.8 KiB
C
62 lines
1.8 KiB
C
|
|
#ifndef TABLEEDITMODEL_H
|
|||
|
|
#define TABLEEDITMODEL_H
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 用来加载显示数据库中数据表的Model类
|
|||
|
|
*
|
|||
|
|
* 基本功能包括:
|
|||
|
|
* 1、可以自定义显示数据表中的哪些列
|
|||
|
|
* 2、可以分页显示并自定义每页展示数量
|
|||
|
|
* 3、编辑的数据可以突出展示(如加粗、改色)
|
|||
|
|
* 4、最前方加入一个用于展示行号的列(行号从1开始)
|
|||
|
|
* 5、点击行号列可以实现选中标识(通过icon)
|
|||
|
|
* 6、哪一行被编辑,改行的行号指示可以加*进行标识
|
|||
|
|
*
|
|||
|
|
*/
|
|||
|
|
|
|||
|
|
#include <QSqlTableModel>
|
|||
|
|
#include <QSqlRecord>
|
|||
|
|
|
|||
|
|
class TableEditModel : public QSqlTableModel
|
|||
|
|
{
|
|||
|
|
Q_OBJECT
|
|||
|
|
public:
|
|||
|
|
explicit TableEditModel(QObject* parent = nullptr, const QSqlDatabase& db = QSqlDatabase(), const QStringList &visibleColumns = {});
|
|||
|
|
~TableEditModel();
|
|||
|
|
|
|||
|
|
QVariant data(const QModelIndex& index, int role) const override;
|
|||
|
|
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
|
|||
|
|
int columnCount(const QModelIndex& parent = QModelIndex()) const override;
|
|||
|
|
Qt::ItemFlags flags(const QModelIndex &index) const override;
|
|||
|
|
|
|||
|
|
//分页控制
|
|||
|
|
void setPageSize(int);
|
|||
|
|
int pageSize() const;
|
|||
|
|
void setCurrentPage(int);
|
|||
|
|
int currentPage() const;
|
|||
|
|
int totalPages() const;
|
|||
|
|
|
|||
|
|
//展示列控制
|
|||
|
|
void setVisibleColumns(const QStringList& columns);
|
|||
|
|
|
|||
|
|
protected:
|
|||
|
|
QString selectStatement() const override; //调用select()时会使用该函数重写后返回的语句
|
|||
|
|
|
|||
|
|
private:
|
|||
|
|
struct EditRecord //编辑记录
|
|||
|
|
{
|
|||
|
|
QSqlRecord original;
|
|||
|
|
QSqlRecord modified;
|
|||
|
|
bool isNew = false;
|
|||
|
|
bool isDeleted = false;
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
int m_pageSize;
|
|||
|
|
int m_currentPage;
|
|||
|
|
int m_totalRecords;
|
|||
|
|
QStringList m_visibleColumns;
|
|||
|
|
QHash<int, EditRecord> m_editCache; //key:当前页中的row
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
#endif //TABLEEDITMODEL_H
|