Qt5开发从入门到精通——第八篇二节( 模型/视图结构—— 模型 (Model))

CSDN话题挑战赛第2期
参赛话题:学习笔记

欢迎小伙伴的点评✨✨,相互学习c/c++应用开发。🍳🍳🍳
博主🧑🧑 本着开源的精神交流Qt开发的经验、将持续更新续章,为社区贡献博主自身的开源精神👩‍🚀

前言

本章节会给大家带来模型/视图结构的 模型 (Model)详解。

一、 模型 (Model) 概述

实现自定义模型可以通过 QAbstractltemModel 类继承,也可以通过 QAbstractListModel 和QAbstractTableModel 类继承实现列表模型或表格模型。
在数据库中,通常需要首先将一些重复的文字字段使用数值代码保存,然后通过外键关联操作来查找其真实的含义,这一方法是为了避免冗余。
Item 主要的角色及其描述
Qt: :DisplayRole :显示文字
Qt::DecorationRole :绘制装饰数据(通常是图标)
Qt::EditRole :在编辑器中编辑的数据
Qt::ToolTipRole :工具提示
Qt::StatusTipRole :状态栏提示
Qt::WhatsThisRole :What’s This 文字
Qt::SizeHintRole :尺寸提示
Qt::FontRole :默认代理的绘制使用的字体
Qt::TextAlignmentRole : 默认代理的对齐方式
Qt::Background.Role :默认代理的背景画刷
Qt::ForegroundRole :默认代理的前景画刷
Qt::CheckStateRole :默认代理的检查框状态
Qt::UserRole :用户自定义的数据的起始位置

二、效果实例

图一
在这里插入图片描述

三、原码解析

modelex.h

#ifndef MODELEX_H
#define MODELEX_H

#include <QWidget>
#include <QAbstractTableModel>
#include <QVector>
#include <QMap>
#include <QStringList>
class ModelEx : public QAbstractTableModel
{
    Q_OBJECT

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


    //虚函数声明
    /*rowCountO 、 columnCountO 、 dataO和返回表头数据的 headerDataO函数是 QAbstract
TableModel 类的纯虚函数。*/
    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 orienation, int role)const;

signals:


public slots:
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

main.cpp

#include "modelex.h"
#include <QApplication>
#include <QTableView>
int main(int argc, char *argv[])
{
    QApplication a(argc,argv);
    ModelEx modelEx;
    QTableView view;
    view.setModel(&modelEx);
    view.setWindowTitle(QObject:: tr("modelEx"));
    view.resize(400,400);
    view.show();
    return a.exec();
}

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("MlAl")<<tr("F一 22");
}

/*columnCountO 函数中,因为模型的列固定为 "3", 所以直接返回 "3" 。*/
int ModelEx::columnCount(const QModelIndex &parent) const
{
    return 3;
}

/*rowCount() 函数返回模型的行数。*/
int ModelEx::rowCount(const QModelIndex &parent) const
{
    return army.size() ;
}


/*data() 函数返回指定索引的数据,即将数值映射为文字。*/
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();
}


/*headerDataO 函数返回固定的表头数据,设置水平表头的标题,*/
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);
}



四、总结

模型/视图结构—— 模型 (Model)会在应用程序开发中经常用到的

  • 10
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 14
    评论
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

东.'

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值