Qt的model/view

Qt中的model/view是源于Smalltalk的Model-View-Controller(MVC)。MVC是将显示的过程分成3个部分:Model专注于数据处理,View专注于界面显示,Controller负责view和model之间的数据传输。这样一个显示过程就分成了三个部分,各司其职,增加了显示的灵活性。而Qt在其基础上做了一定的修改,使其变的简单而又灵活。


Qt 的model/view模型如上图所示,Model用来读取和存储数据,View则调用Model来显示Model中存储的数据,而Delegate充当了conttol的作用。如果不需要修改数据,则不需要调用delegate,只需要model和view两个类就可以了,这样更加的简化了代码的编写。下面是简单的显示过程实例。

class TelecomTableModel: public QAbstractTableModel
{
public:
    TelecomTableModel(QObject* parent = 0);
//dat:数据的容器,start:开始复制的位置,end:结束复制的最后一个元素的下一个位置
    void setInfoData(QVector<showData> &dat,int start,int end);
//返回数据的列和行
    int rowCount(const QModelIndex &parent) const;
    int columnCount(const QModelIndex &parent) const;
//显示数据
    QVariant data(const QModelIndex &index, int role) const;
//显示数据头
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const;
public:
int numOfPage;//页数从0开始计算
int numOfData;
QVector<showData> dataHis;
private:
    int row;
    int column;
    QVector<showData> qvData;//model中存放数据
};

在这个类中需要关注三个函数:rowCount(...),columnCount(...),data(...),他们都是QAbstractTableModel的虚函数。

rowCount(...)和columnCount(...)是用来确定View需要显示的行数和列数,这个必须要实现,否则view将不知道需要显示多少数据,程序也将出错;

int alarmTableModel::rowCount(const QModelIndex &parent) const
{
return qvData.count();
}


int alarmTableModel::columnCount(const QModelIndex &parent) const
{
    return 5;
}

data(const QModelIndex &index, int role)用来具体的显示数据,index是view遍历由rowCount(...)和columnCount(...)决定的显示范围内的每一个单元格,而role则用来表示本次设定的是单元的值、对齐方式等;而返回值则用来设定单元格的属性或值。

QVariant alarmTableModel::data(const QModelIndex &index, int role) const
{
showData temp;
QString stemp;
int row,column;


row = index.row();
column = index.column();
//qDebug()<<"-----data-----";
if(!index.isValid())
return QVariant();
if(role == Qt::TextAlignmentRole){
//qDebug()<<"TextAlignmentRole";
return int(Qt::AlignLeft | Qt::AlignVCenter);
}else if(role == Qt::DisplayRole){
//qDebug()<<"displayRole";

temp = qvData.at(row);
switch(column){
case 0:
stemp = temp.IEDName;
break;
case 1:
stemp = temp.LDName;
break;
case 2:
stemp = temp.signalName;
break;
case 3:
stemp = temp.happenTime.toString("yyyy-MM-dd hh:mm:ss:zzz");
break;
case 4:
if(temp.comState == 0)
stemp = QString(tr("state: 0"));
else
stemp = QString(tr("state: 1"));
break;
}
}

    return stemp;
}

因为本类继承自QAbstractTableModel,所以还需要一个headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole)函数,section表示选择的序号,而orientation表示序号所在的方向。

QVariant alarmTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
//qDebug()<<"-----headData-----";
if(role != Qt::DisplayRole){
//qDebug()<<"display role";
return QVariant();
}


//return QString::number(section);
if(orientation == Qt::Horizontal){
switch(section){
case 0:
return QString("IED Name");
//break;
case 1:
return QString("LD Name");
//break;
case 2:
return QString("Signal Name");
//break;
case 3:
return QString("Time");
//break;
case 4:
return QString("State");
//break;
}
}else{
if((numOfPage)*(confp.numOfEveryPage) < numOfData)
return QString("%1").arg(section + 1 + (numOfPage)*(confp.numOfEveryPage));
else
return QString("%1").arg(section + 1 + (numOfPage-1)*(confp.numOfEveryPage));
}
    //return QString("%1").arg(section);
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值