引言
实现自定义模型可以通过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,