QT模型/视图结构之模型(model)

引言

实现自定义模型可以通过QAbstracItemModel类继承,也可以通过QAbstractListModel和QAbstractTableModel类继承实现列表模型或表格模型。

示例代码

通过实现将数值代码转换为文字的模型来介绍如何使用自定义模型。
在这里插入图片描述

自定义模型modelEx头文件

#pragma once

#include <QAbstractTableModel>
#include <QVector>
#include <QMap>
#include <QStringList>

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

    virtual int rowCount(const QModelIndex& parent = QModelIndex()) const;
    virtual int columnCount(const QModelIndex& parent = QModelIndex()) const;

    virtual QVariant data(const QModelIndex& index, int role)const;
    virtual QVariant headerData(int section, Qt::Orientation orientation, int role)const;
signals:

public slots:
private:
    QVector<short> army;
    QVector<short> weaponType;

    QMap<short, QString> armyMap;
    QMap<short, QString> weaponTypeMap;

    QStringList weapon;
    QStringList header;

    void populateModel();

};

  • rowCount(),columnCount()、data()和返回表头数据headerData()函数是QAbstractTableModel类的纯虚函数。

modelEx 自定义模型实现

#include "modelEx.h"

#pragma execution_charater_set("utf-8");

modelEx::modelEx(QObject* parent)
    : QAbstractTableModel(parent)
{
    armyMap[1] = tr("HJ");
    armyMap[2] = tr("KJ");
    armyMap[3] = tr("LJ");
    armyMap[4] = tr("HJLZD");

    weaponTypeMap[1] = tr("HZJ");
    weaponTypeMap[2] = tr("ZDJ");
    weaponTypeMap[3] = tr("HKMJ");
    weaponTypeMap[4] = tr("QZJ");
    weaponTypeMap[5] = tr("ZSJ");
    weaponTypeMap[6] = tr("TK");
    weaponTypeMap[7] = tr("LXGJJ");
    weaponTypeMap[8] = tr("LXZC");

    populateModel();
}


void modelEx::populateModel()
{
    header << QString::fromLocal8Bit("军种") << QString::fromLocal8Bit("种类") << QString::fromLocal8Bit("武器");
    army << 1 << 2 << 3 << 4 << 3 << 2 << 1;
    weaponType << 1 << 3 << 5 << 7 << 4 << 8 << 6 << 2;
    weapon << QString::fromLocal8Bit("B-2") << QString::fromLocal8Bit("尼米兹级") << QString::fromLocal8Bit("阿帕奇") << QString::fromLocal8Bit("黄蜂级")
        << QString::fromLocal8Bit("阿利伯克其") << QString::fromLocal8Bit("AAAV") << QString::fromLocal8Bit("M1A1") << QString::fromLocal8Bit("F-22");
}

int modelEx::rowCount(const QModelIndex& parent) const
{
    return army.size();
}
int modelEx::columnCount(const QModelIndex& parent) const
{
    return 3;
}

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();
        }
    }
    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);
}

  • role == Qt::DisplayRole:模型中条目能够显示不同的角色,这样可以在不同的情况下显示不同的数据。Qt::DisplayRole用来存取视图中显示的文字,角色由枚举类Qt::ItemDataRole定义,主要角色及描述如下:
    enum ItemDataRole {
        DisplayRole = 0, // 显示文字
        DecorationRole = 1,// 绘制装饰数据
        EditRole = 2,// 在编辑器中编辑的数据
        ToolTipRole = 3,//工具提示
        StatusTipRole = 4,// 状态栏提示
        WhatsThisRole = 5,// what's This文字
        // Metadata
        FontRole = 6,// 默认代理的绘制使用字体
        TextAlignmentRole = 7,//默认代理的对齐方式
        BackgroundColorRole = 8,//
        BackgroundRole = 8,// 默认代理的背景画刷
        TextColorRole = 9,
        ForegroundRole = 9,// 默认代理的前景画刷
        CheckStateRole = 10,//默认代理的检查框状态
        // Accessibility
        AccessibleTextRole = 11,
        AccessibleDescriptionRole = 12,
        // More general purpose
        SizeHintRole = 13,// 尺寸提示
        InitialSortOrderRole = 14,
        // Internal UiLib roles. Start worrying when public roles go that high.
        DisplayPropertyRole = 27,
        DecorationPropertyRole = 28,
        ToolTipPropertyRole = 29,
        StatusTipPropertyRole = 30,
        WhatsThisPropertyRole = 31,
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

华衣在盛

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

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

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

打赏作者

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

抵扣说明:

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

余额充值