QT自定义TableModel

1、modelex.h

#ifndef MODELEX_H
#define MODELEX_H
#include <QAbstractTableModel>
#include <QVector>
#include <QMap>
#include <QStringList>


class ModelEx:public QAbstractTableModel
{
public :
        //构造函数
        ModelEx(QObject *parent=0);
        //行数
        virtual int rowCount(const QModelIndex &parent) const;
        //列数
        virtual int columnCount(const QModelIndex &parent) const;
        //数据对象
        QVariant data(const QModelIndex &index, int role) const;
        //头部数据
        QVariant headerData(int section, Qt::Orientation orientation, int role) const;

private :
        QVector<short> army;
        QVector<short> weaponType;
        QMap<short,QString> armyMap;
        QMap<short,QString> weaponTypeMap;
        QStringList weapon;
        QStringList header;

        void populateModel();

};

#endif // MODELEX_H


2、modelex.cpp

#include "modelex.h"

ModelEx::ModelEx(QObject *parent)
    :QAbstractTableModel(parent)
{
    armyMap[1]=tr("空军");
    armyMap[2]=tr("海军");
    armyMap[3]=tr("陆军");
    armyMap[4]=tr("海军陆战队");

    weaponTypeMap[1]=tr("轰炸机");
    weaponTypeMap[2]=tr("战斗机");
    weaponTypeMap[3]=tr("航空母舰");
    weaponTypeMap[4]=tr("驱逐舰");
    weaponTypeMap[5]=tr("直升机");
    weaponTypeMap[6]=tr("坦克");
    weaponTypeMap[7]=tr("两栖攻击舰");
    weaponTypeMap[8]=tr("两栖战车");
    populateModel();

}
void ModelEx::populateModel()
{
    header<<tr("军种")<<tr("种类")<<tr("武器");
    army<<1<<2<<3<<4<<2<<4<<3<<1;//军种
    weaponType<<1<<3<<5<<7<<4<<8<<6<<2;//武器种类

    //武器
    weapon<<tr("B-2")<<tr("尼来兹级")<<tr("阿帕奇")<<tr("黄蜂级")
            <<tr("阿利伯克级")<<tr("AAAV")<<tr("M1A1")<<tr("F-22");
}
int ModelEx::columnCount(const QModelIndex &parent) const
{
    return 3;
}

int ModelEx::rowCount(const QModelIndex &parent) const
{
    return army.size();
}
QVariant ModelEx::data(const QModelIndex &index, int role) const
{
    if(!index.isValid())
    {
        return QVariant();

    }
    if(role==Qt::DisplayRole)
    {
        switch(index.column())
        {
        case 0:
            return armyMap[army[index.row()]];
            break;
        case 1:
            {
                return weaponTypeMap[weaponType[index.row()]];
                break;

            }
        case 2:
            {
                return weapon[index.row()];
            }
        default:
            return QVariant();
        }
    }
    return QVariant();
}

QVariant ModelEx::headerData(int section, Qt::Orientation orientation, int role) const
{
    if(role==Qt::DisplayRole && orientation==Qt::Horizontal)
    {
        return header[section];
    }
    return QAbstractTableModel::headerData(section,orientation,role);
}



3、main.cpp

#include <QApplication>
#include "modelex.h"
#include <QTextCodec>
#include <QTableView>
int main(int argc,char* argv[])
{

    QApplication app(argc,argv);
    QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
    ModelEx modelex;
    QTableView view;

    view.setModel(&modelex);
    view.setWindowTitle(QObject::tr("ModelEx"));
    view.resize(500,500);
    view.show();
    return app.exec();
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值