第8章:Model

1,QT模型/视图中的Model

2,源码:

#ifndef MODELEX_H
#define MODELEX_H

#include <QAbstractTableModel>
#include <QObject>

class ModelEx : public QAbstractTableModel
{
public:
    explicit ModelEx(QObject *parent=0);

    //下面四个函数是QAbstractTAbleModel类的纯虚函数
    virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;
    virtual int columnCount(const QModelIndex &parent = QModelIndex()) 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数据保存数值和文字的映射
    QMap<short, QString> weaponTypeMap;

    QStringList  weapon;
    QStringList  header;

    void populateModel();

};

#endif // MODELEX_H
#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()];
            break;

            default:
                return QVariant();
            break;
        }
    }

    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);
}
#include "mainwindow.h"
#include <QApplication>
#include <QTableView>
#include "modelex.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
//    MainWindow w;
//    w.show();

    ModelEx     modelEx;
    QTableView  view;

    view.setModel(&modelEx);
    view.setWindowTitle(QObject::tr("modelEx"));
    view.resize(400, 400);

    view.show();

    return a.exec();
}

3,效果:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/ksmtnsv37297/article/details/86621375