神秘博客

QSqlTableModel添加复选框

我们首先只需要继承QSqlTableModel然后重新实现下面几个函数即可
data() 读取数据库数据返回对应的值
setData() 设置数据库的值
flags()设置用户可选角色,绘制出QCheckBox

另外我还加了保存复选框状态到文件和读取文件

#pragma once

#include <QObject>
#include <qsqltablemodel.h>
#include <QMap>


class SqlTableModel :public QSqlTableModel

{
  Q_OBJECT

public:
  explicit SqlTableModel(const int &i, QObject *parent = nullptr, QSqlDatabase db = QSqlDatabase());
  bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);
  QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
  Qt::ItemFlags flags(const QModelIndex &index) const;
  ~SqlTableModel();
  Qt::CheckState &operator[](const int &i) { return checkState[i]; }  //外部返回复选框状态
  
private:
  QMap<int, Qt::CheckState> checkState;  //保存复选框状态
  int checkValue;  //记录第几个TableView
};

#include "SqlTableModel.h"
#include <QDebug>
#include <qsqlrecord.h>
#include <QFile>
#include <qtextstream.h>

SqlTableModel::SqlTableModel(const int &i, QObject * parent, QSqlDatabase db) :checkValue(i),QSqlTableModel(parent, db)
{
  if (checkValue == 1)
  {
    //读取选中状态
    QFile file("data1.dat");
    if (file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
      QTextStream in(&file);
      while (!in.atEnd())
      {
        int a, b;
        in >> a;
        in >> b;
        checkState[a] = (b == 2 ? Qt::Checked : Qt::Unchecked);
        in.readLine();
      }
    }
    file.close();
  }
  else if (checkValue == 2)
  {
    //读取选中状态
    QFile file("data2.dat");
    if (file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
      QTextStream in(&file);
      while (!in.atEnd())
      {
        int a, b;
        in >> a;
        in >> b;
        checkState[a] = (b == 2 ? Qt::Checked : Qt::Unchecked);
        in.readLine();
      }
    }
    file.close();
  }
}

// setData()方法主要用来设置是否被选中,然后将对应的状态保存到QMap中
bool SqlTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
  if (index.column() == 0 && role == Qt::CheckStateRole)
  {

    checkState[index.row()] = (value == Qt::Checked ? Qt::Checked : Qt::Unchecked);
    return true;
  }
  else
  {
    return QSqlTableModel::setData(index, value, role);
  }

}
// data()方法主要用来显示,取出QMap中的值,返回对应的状态
QVariant SqlTableModel::data(const QModelIndex &index, int role) const
{
  if (!index.isValid())
    return QVariant();
  if (index.column() == 0 && role == Qt::CheckStateRole)
  {
    if (checkState.contains(index.row()))
      return checkState[index.row()];
    return Qt::Unchecked;
  }
  else
    return QSqlTableModel::data(index, role);
}
//flag()方法主要设置用户可选角色,绘制出QCheckBox
Qt::ItemFlags SqlTableModel::flags(const QModelIndex &index) const
{
  if (!index.isValid())
    return Qt::ItemIsEnabled;
  if (index.column() == 0)
    return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable| QSqlTableModel::flags(index);
  else
    return QSqlTableModel::flags(index);
}

//析构函数,结束时保存复选框状态
SqlTableModel::~SqlTableModel()
{
  if (checkValue == 1)
  {
    //保存项目勾选
    QFile fileout("data1.dat");
    if (fileout.open(QIODevice::WriteOnly | QIODevice::Text))
    {
      QTextStream out(&fileout);
      for (int i = 0; i < checkState.size(); i++)
      {
        out << i << " ";
        out << checkState[i] << endl;
      }
    }
    fileout.close();
  }
  else if (checkValue == 2)
  {
    //保存项目勾选
    QFile fileout("data2.dat");
    if (fileout.open(QIODevice::WriteOnly | QIODevice::Text))
    {
      QTextStream out(&fileout);
      for (int i = 0; i < checkState.size(); i++)
      {
        out << i << " ";
        out << checkState[i] << endl;
      }
    }
    fileout.close();
  }
  
}
版权说明:
点赞

发表评论

电子邮件地址不会被公开。 必填项已用*标注

觉得文章有用就请我吃包辣条吧

微信扫一扫打赏