创建基于QVariant的泛型模型可以通过以下步骤实现:
#include <QAbstractItemModel>
#include <QVariant>
#include <QList>
class GenericItem
{
public:
GenericItem(const QVariant& data, GenericItem* parent = nullptr);
~GenericItem();
void appendChild(GenericItem* child);
GenericItem* child(int row);
int childCount() const;
int columnCount() const;
QVariant data(int column) const;
int row() const;
GenericItem* parent();
private:
QList<GenericItem*> m_children;
QVariant m_itemData;
GenericItem* m_parent;
};
GenericItem::GenericItem(const QVariant& data, GenericItem* parent)
: m_itemData(data), m_parent(parent)
{
}
GenericItem::~GenericItem()
{
qDeleteAll(m_children);
}
void GenericItem::appendChild(GenericItem* child)
{
m_children.append(child);
}
GenericItem* GenericItem::child(int row)
{
return m_children.value(row);
}
int GenericItem::childCount() const
{
return m_children.count();
}
int GenericItem::columnCount() const
{
return 1; // 只有一列数据
}
QVariant GenericItem::data(int column) const
{
if (column == 0)
return m_itemData;
return QVariant();
}
int GenericItem::row() const
{
if (m_parent)
return m_parent->m_children.indexOf(const_cast<GenericItem*>(this));
return 0;
}
GenericItem* GenericItem::parent()
{
return m_parent;
}
class GenericModel : public QAbstractItemModel
{
public:
GenericModel(QObject* parent = nullptr);
~GenericModel();
QModelIndex index(int row, int column, const QModelIndex& parent = QModelIndex()) const override;
QModelIndex parent(const QModelIndex& index) const override;
int rowCount(const QModelIndex& parent = QModelIndex()) const override;
int columnCount(const QModelIndex& parent = QModelIndex()) const override;
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
private:
GenericItem* m_rootItem;
};
GenericModel::GenericModel(QObject* parent)
: QAbstractItemModel(parent), m_rootItem(nullptr)
{
}
GenericModel::~GenericModel()
{
delete m_rootItem;
}
QModelIndex GenericModel::index(int row, int column, const QModelIndex& parent) const
{
if (!hasIndex(row, column, parent))
return QModelIndex();
GenericItem* parentItem;
if (!parent.isValid())
parentItem = m_rootItem;
else
parentItem = static_cast<GenericItem*>(parent.internalPointer());
GenericItem* childItem = parentItem->child(row);
if (childItem)
return createIndex(row, column, childItem);
else
return QModelIndex();
}
QModelIndex GenericModel::parent(const QModelIndex& index) const
{
if (!index.isValid())
return QModelIndex();
GenericItem* childItem = static_cast<GenericItem*>(index.internalPointer());
GenericItem* parentItem = childItem->parent();
if (parentItem == m_rootItem)
return QModelIndex();
return createIndex(parentItem->row(), 0, parentItem);
}
int GenericModel::rowCount(const QModelIndex& parent) const
{
if (parent.column() > 0)
return 0;
GenericItem* parentItem;
if (!parent.isValid())
parentItem = m_rootItem;
else
parentItem = static_cast<GenericItem*>(parent.internalPointer());
return parentItem->childCount();
}
int GenericModel::columnCount(const QModelIndex& parent) const
{
if (parent.isValid())
return static_cast<GenericItem*>(parent.internalPointer())->columnCount();
else
return 1; // 只有一列数据
}
QVariant GenericModel::data(const QModelIndex& index, int role) const
{
if (!index.isValid())
return QVariant();
if (role != Qt::DisplayRole)
return QVariant();
GenericItem* item = static_cast<GenericItem*>(index.internalPointer());
return item->data(index.column());
}
GenericModel* model = new GenericModel(this);
GenericItem* rootItem = new GenericItem("Root Item");
model->setRootItem(rootItem);
GenericItem* childItem1 = new GenericItem("Child Item 1", rootItem);
rootItem->appendChild(childItem1);
GenericItem* childItem2 = new GenericItem("Child Item 2", rootItem);
rootItem->appendChild(childItem2);
通过以上步骤,你可以创建一个基于QVariant的泛型模型,并在模型中存储和展示各种类型的数据。
领取专属 10元无门槛券
手把手带您无忧上云