//EasyTableModel.cpp #include "EasyTableModel.h" #include EasyTableModel::EasyTableModel(QObject *parent) : QAbstractTableModel(parent) { } QStringList EasyTableModel::getHorHeader() const { return _horHeaderList; } void EasyTableModel::setHorHeader(const QStringList &header) { _horHeaderList=header; emit horHeaderChanged(); } QString EasyTableModel::getTable() const { return _table_name; } void EasyTableModel::setTable(const QString &strA) { _table_name = strA; emit initTableChanged(); } QJsonArray EasyTableModel::getInitData() const { return _initData; } void EasyTableModel::setInitData(const QJsonArray &jsonArr) { _initData=jsonArr; //设置最大值 if(_completed){ loadData(_initData,_table_name); } emit initDataChanged(); } void EasyTableModel::classBegin() { DEBUG("EasyTableModel::classBegin()"); } void EasyTableModel::componentComplete() { DEBUG("EasyTableModel::componentComplete()"); _completed=true; if(!_initData.isEmpty()){ loadData(_initData,_table_name); } } QHash EasyTableModel::roleNames() const { //value表示取值,edit表示编辑 return QHash{ { Qt::DisplayRole,"value" }, { Qt::EditRole,"edit" } }; } QVariant EasyTableModel::headerData(int section, Qt::Orientation orientation, int role) const { //返回表头数据,无效的返回None if(role==Qt::DisplayRole){ if(orientation==Qt::Horizontal){ return _horHeaderList.value(section,QString::number(section)); }else if(orientation==Qt::Vertical){ return QString::number(section); } } return QVariant(); } bool EasyTableModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role) { if (value != headerData(section, orientation, role)) { if(orientation==Qt::Horizontal&&role==Qt::EditRole){ _horHeaderList[section]=value.toString(); emit headerDataChanged(orientation, section, section); return true; } } return false; } int EasyTableModel::rowCount(const QModelIndex &parent) const { if (parent.isValid()) return 0; return _modelData.count(); } int EasyTableModel::columnCount(const QModelIndex &parent) const { if (parent.isValid()) return 0; return _horHeaderList.count(); } QVariant EasyTableModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) return QVariant(); switch (role) { case Qt::DisplayRole: case Qt::EditRole: return _modelData.at(index.row()).at(index.column()); default: break; } return QVariant(); } bool EasyTableModel::setData(const QModelIndex &index, const QVariant &value, int role) { if (value.isValid()&&index.isValid()&&(data(index, role) != value)) { if(Qt::EditRole==role){ _modelData[index.row()][index.column()]=value; DEBUG("_modelData: row={} col={}", index.row(), index.column()); //emit dataChanged(index, index, QVector() << role); // emit dataChanged(); return true; } } return false; } Qt::ItemFlags EasyTableModel::flags(const QModelIndex &index) const { if (!index.isValid()) return Qt::NoItemFlags; return Qt::ItemIsEnabled|Qt::ItemIsSelectable|Qt::ItemIsEditable; } void EasyTableModel::loadData(const QJsonArray &data,QString &table_name) { DEBUG("tbname: {}", table_name.toStdString()); //如果要区分类型的话,可以用role, //这样ui中就能使用model.role来获取对应index的参数 QVector> newData; QJsonArray::const_iterator iter; for(iter=data.begin();iter!=data.end();++iter){ QVector newRow; const QJsonObject itemRow=(*iter).toObject(); if(table_name != QString::fromLocal8Bit("camera")) newRow.append(itemRow.value("id")); if(table_name == QString::fromLocal8Bit("mqtt") ||table_name == QString::fromLocal8Bit("mqtt_other")) { newRow.append(itemRow.value("dtype")); newRow.append(itemRow.value("ip")); newRow.append(itemRow.value("port")); newRow.append(itemRow.value("imei")); newRow.append(itemRow.value("uid")); newRow.append(itemRow.value("pwd")); newRow.append(itemRow.value("clientid")); newRow.append(itemRow.value("qos")); newRow.append(itemRow.value("keepalive")); newRow.append(itemRow.value("sub")); newRow.append(itemRow.value("pub")); newRow.append(itemRow.value("offline")); } else if(table_name == QString::fromLocal8Bit("ftp") ||table_name == QString::fromLocal8Bit("ftp_other")) { newRow.append(itemRow.value("ip")); newRow.append(itemRow.value("port")); newRow.append(itemRow.value("dir")); newRow.append(itemRow.value("uid")); newRow.append(itemRow.value("pwd")); newRow.append(itemRow.value("mode")); } else if(table_name == QString::fromLocal8Bit("location")) { newRow.append(itemRow.value("lbs_lat")); newRow.append(itemRow.value("lbs_lng")); newRow.append(itemRow.value("gps_lat")); newRow.append(itemRow.value("gps_lng")); newRow.append(itemRow.value("gps_elevation")); }else if(table_name == QString::fromLocal8Bit("camera")){ newRow.append(itemRow.value("camera_type")); newRow.append(itemRow.value("mode")); newRow.append(itemRow.value("stitch_photonum")); newRow.append(itemRow.value("photo_width")); newRow.append(itemRow.value("photo_height")); newRow.append(itemRow.value("auto_focus")); newRow.append(itemRow.value("movz_f")); newRow.append(itemRow.value("movz_b")); newRow.append(itemRow.value("movx_f")); newRow.append(itemRow.value("movx_b")); } else if(table_name == QString::fromLocal8Bit("data")) { newRow.append(itemRow.value("datetime")); newRow.append(itemRow.value("Json_data")); newRow.append(itemRow.value("Json_warn")); } //ftp newData.append(newRow); } emit beginResetModel(); _modelData=newData; emit endResetModel(); }