#ifndef PARAMETERSMODEL_H#define PARAMETERSMODEL_H#include <QAbstractTableModel>class ParametersModel : public QAbstractTableModel{ Q_OBJECTpublic: explicit ParametersModel(QObject *parent = nullptr); void populateData(const QList<QString> &Measurment, const QList<QString> &Unit, const QList<QString> &Current); int rowCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE; int columnCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE; QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE;private: QList<QString> m_Measurment; QList<QString> m_Unit; QList<QString> m_Current;};#endif // PARAMETERSMODEL_H
#include "parametersmodel.h"ParametersModel::ParametersModel(QObject *parent) : QAbstractTableModel(parent){ }void ParametersModel::populateData(const QList<QString> &Measurment, const QList<QString> &Unit, const QList<QString> &Current){ m_Measurment.clear(); m_Measurment = Measurment; m_Unit.clear(); m_Unit = Unit; m_Current.clear(); m_Current = Current; emit dataChanged(index(0, 0), index(rowCount(), columnCount())); return;}int ParametersModel::rowCount(const QModelIndex &parent) const{ Q_UNUSED(parent); return m_Measurment.length();}int ParametersModel::columnCount(const QModelIndex &parent) const{ Q_UNUSED(parent); return 3;}QVariant ParametersModel::data(const QModelIndex &index, int role) const{ if (!index.isValid() || role != Qt::DisplayRole) { return QVariant(); } if (index.column() == 0) { return m_Measurment[index.row()]; } else if (index.column() == 1) { return m_Unit[index.row()]; } else if (index.column() == 2) { return m_Current[index.row()]; } return QVariant();}QVariant ParametersModel::headerData(int section, Qt::Orientation orientation, int role) const{ if (role == Qt::DisplayRole && orientation == Qt::Horizontal) { if (section == 0) { return QString("Измерение"); } else if (section == 1) { return QString("ед. изм."); } else if (section == 2) { return QString("Текущее значение"); } } return QVariant();}
paramModel->populateData(Measurment, Unit, Current);
ParametersModel::data
void MainWindow::updateTable(){ QList<QString> Measurment; QList<QString> Unit; QList<QString> Current; Measurment.append("X"); Unit.append("X"); Current.append(QString::number(X, 'f', 3)); Measurment.append("Y"); Unit.append("Y"); Current.append(QString::number(Y, 'f', 3)); Measurment.append("Z"); Unit.append("Z"); Current.append(QString::number(Z, 'f', 3)); paramModel->populateData(Measurment, Unit, Current);}