qt之model

#ifndef QBASETABLEMODEL_H
#define QBASETABLEMODEL_H


#include <QAbstractTableModel>
#include <QList>
#include <QIcon>
#include "libviewbase_global.h"


#define FUNCODE_VIEW_COLUMN  8


enum enColProperty //tableview列属性
{
cp_select = 0, //选择
cp_index,      //序号
cp_name,    //名称
cp_currentVal, //当前值
cp_minVal,     //最小
cp_maxVal,     //最大
cp_defaultVal, //默认值
cp_unit,       //单位7
cp_writeLevel, //写级别
cp_showLevel   //显示级别
};


class LIBVIEWBASE_EXPORT QBaseTableModel : public QAbstractTableModel
{
Q_OBJECT


public:
QBaseTableModel(QObject *parent = NULL);
~QBaseTableModel();


public:
virtual void setHeadList(QStringList &headList);//头部数据
virtual int rowCount(const QModelIndex &parent = QModelIndex()) const override;
virtual int columnCount(const QModelIndex &parent = QModelIndex()) const override;
virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const;
virtual QVariant data(const QModelIndex &index, int role) const;
virtual Qt::ItemFlags flags(const QModelIndex &index) const override;
virtual void setModelData(const QList<QStringList> &itemList);
virtual void insertOneData(const QStringList &item);
virtual void refreshAll();


signals:


public:
QStringList getHeadList() { return m_headList; }
QList<QStringList> getItemList() { return m_itemList; }


protected:
QIcon getModLevelIcon(int modLevel, int showLevel) const;
QString getModelLevelTip(int modLevel, int showLevel) const;
QColor getBGColorByRow(int row) const;//获取不同行的背景色


protected:
QStringList m_headList;
QList<QStringList> m_itemList;
};


#endif // QBASETABLEMODEL_H


#include "QBaseTableModel.h"
#include "libDatabaseInc.h"
#include <QBrush>


QBaseTableModel::QBaseTableModel(QObject *parent)
: QAbstractTableModel(parent)
{


}


QBaseTableModel::~QBaseTableModel()
{


}


QIcon QBaseTableModel::getModLevelIcon(int modLevel, int showLevel) const
{
if (modLevel == ml_allPermit)
{
if (showLevel == sl_none)
{
return QIcon(":/img/Funcode/rw_4");
}
else
{
return QIcon(":/img/Funcode/rw_0");
}
}
else if (modLevel == ml_runForbid)
{
return QIcon(":/img/Funcode/rw_1");
}
else
{
if (showLevel == sl_none)
{
return QIcon(":/img/Funcode/rw_3");
}
else
{
return QIcon(":/img/Funcode/rw_2");
}
}
}


QString QBaseTableModel::getModelLevelTip(int modLevel, int showLevel) const
{
QString str;
if (modLevel == ml_allPermit)
{
if (showLevel == sl_none)
{
str = tr("STR_MODIYF_WRITEONLY");
}
else
{
str = tr("STR_MODIYF_ANYTIME");
}
}
else if (modLevel == ml_runForbid)
{
str = tr("STR_MODIYF_NOTRUN");
}
else
{
if (showLevel == sl_none)
{
str = tr("STR_MODIYF_FORBID");
}
else
{
str = tr("STR_MODIFY_READONLY");
}
}


return str;
}


QColor QBaseTableModel::getBGColorByRow(int row) const
{
if (row % 2 == 0)
{
return QColor(220, 235, 255);
}
else
{
return QColor(238, 238, 242);
}
}


void QBaseTableModel::setHeadList(QStringList &headList)
{
m_headList = headList;
}


int QBaseTableModel::rowCount(const QModelIndex &parent /* = QModelIndex() */) const
{
return m_itemList.size();
}


int QBaseTableModel::columnCount(const QModelIndex &parent /* = QModelIndex() */) const
{
return m_headList.size();
}


QVariant QBaseTableModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
{
return QVariant();
}


int row = index.row();
int colum = index.column();


if (row < 0 || row >= m_itemList.size())
{
return QVariant();
}


switch (role)
{
case Qt::TextAlignmentRole:
{
return QVariant(Qt::AlignLeft | Qt::AlignVCenter/*AlignCenter*/);
}
/*case Qt::BackgroundRole:
{
return QBrush(getBGColorByRow(row));
}*/
case Qt::DisplayRole:
case Qt::ToolTipRole:
{
return m_itemList[row].at(colum);
}


default:
break;
}


return QVariant();
}


Qt::ItemFlags QBaseTableModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return 0;


return QAbstractItemModel::flags(index);
}


void QBaseTableModel::setModelData(const QList<QStringList> &itemList)
{
beginResetModel();
m_itemList = itemList;
endResetModel();
}


void QBaseTableModel::insertOneData(const QStringList &item)
{
beginResetModel();
m_itemList.push_front(item);
endResetModel();
}


void QBaseTableModel::refreshAll()
{
beginResetModel();
endResetModel();
}


QVariant QBaseTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
switch (role)
{
case Qt::TextAlignmentRole:
{
return QVariant(Qt::AlignLeft | Qt::AlignVCenter/*AlignCenter*/);
}


case Qt::DisplayRole:
{
if (orientation == Qt::Horizontal)
{
if (m_headList.size() > section)
{
return m_headList[section];
}
else
{
return QVariant();
}
}
break;
}


default:
break;
}


return QVariant();
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值