emsApplication/applications/emsConfigurer/mytablemodel.cpp

152 lines
3.9 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <QColor>
#include "mytablemodel.h"
MyTableModel::MyTableModel(QObject* parent)
:QAbstractTableModel(parent)
{
for(int i=0;i<100;i++)
{
ModelItem item;
item.status = STATUS_WARN;
item.ParameterName= "Visual Leak Detector detected 1 memory leak (84 bytes)";
item.sampleTime = "2024-9-10 11:22:33.444";
item.unit = "C";
item.value = "100";
m_modelData.append(item);
}
}
int MyTableModel::rowCount(const QModelIndex & /*parent*/) const
{
return m_modelData.size();
}
int MyTableModel::columnCount(const QModelIndex & /*parent*/) const
{
return AppData::getInstance()->lstDataTableHeaderText.count();
}
QVariant MyTableModel::data(const QModelIndex& index, int role) const
{
if (!index.isValid())
return QVariant();
if (role == Qt::DisplayRole || role == Qt::EditRole)
{
const int row = index.row();
switch (index.column())
{
case 0:
{
if (m_modelData.at(row).status == STATUS_NORMAL) return tr("Normal");
if (m_modelData.at(row).status == STATUS_INFO) return tr("INFO");
if (m_modelData.at(row).status == STATUS_WARN) return tr("WARN");
if (m_modelData.at(row).status == STATUS_ERROR) return tr("ERROR");
}
case 1:
{
return m_modelData.at(row).ParameterName.c_str();
}
case 2: return m_modelData.at(row).value.c_str();
case 3: return m_modelData.at(row).unit.c_str();
case 4: return m_modelData.at(row).sampleTime.c_str();
}
}
//对齐处理
if (role == Qt::TextAlignmentRole)
{
//const int row = index.row();
switch (index.column())
{
case 1:
case 2:
case 3:
return QVariant(Qt::AlignLeft|Qt::AlignVCenter);
break;
//case 4:
// return QVariant(Qt::AlignRight|Qt::AlignVCenter);
// break;
default:
return Qt::AlignCenter;
break;
}
}
if (role == Qt::BackgroundRole)
{
const int row = index.row();
switch (index.column())
{
case 0: //状态
case 2: //数值
{
if (m_modelData.at(row).status == STATUS_WARN)
return QVariant(QColor(Qt::yellow));
else if (m_modelData.at(row).status == STATUS_ERROR)
return QVariant(QColor(Qt::red));
}
}
}
return QVariant();
}
QVariant MyTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role == Qt::DisplayRole && orientation == Qt::Horizontal)
{
if (section < m_headeList.size())
{
return m_headeList[section];
}
}
return QAbstractItemModel::headerData(section, orientation, role);
}
const TableData *MyTableModel::getModelData() const
{
return &m_modelData;
}
bool MyTableModel::setData(const QModelIndex& index, const QVariant& value, int role)
{
//beginResetModel();
if (!index.isValid())
return false;
//endResetModel(); //在结束前添加此函数
return QAbstractTableModel::setData(index, value, role);
}
Qt::ItemFlags MyTableModel::flags(const QModelIndex &index) const
{
return Qt::ItemIsEditable | QAbstractTableModel::flags(index);
}
void MyTableModel::setModelData(TableData data)
{
//修改数据前后用beginResetModel()和endResetModel()
beginResetModel(); //通过这个告诉我要开始修改model了
m_modelData.clear();
for (int i = 0;i < data.count();++i)
{
m_modelData.append(data[i]);
}
endResetModel(); //通过这个告诉我修改model结束了
}
void MyTableModel::clearModelData()
{
m_modelData.clear();
}
void MyTableModel::setHeadData(QStringList i_list)
{
m_headeList=i_list;
}