这里想要写一个QAbstractListModel的子类,学习一下如何实例化QAbstractListModel。
QAbstractListModel子类化-CSDN博客
QVariant与自定义类型互转之奇巧淫技_qt 类型转 qvariant-CSDN博客
#pragma once
#include <QStyledItemDelegate>
#include <qmetatype.h>
class QListView;
class WItemDelegate1 : public QStyledItemDelegate
{
Q_OBJECT
public:
WItemDelegate1(QListView* parent = nullptr);
~WItemDelegate1();
void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const override;
QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const override;
};
#include "WItemDelegate1.h"
#include "qlistview.h"
#include "qpainter.h"
#include "qsqlquerymodel.h"
#include <qdebug.h>
#include "ListModel.h"
WItemDelegate1::WItemDelegate1(QListView* parent)
: QStyledItemDelegate(parent)
{
}
WItemDelegate1::~WItemDelegate1()
{}
QSize WItemDelegate1::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
{
return QSize(option.rect.width(), 60);
}
void WItemDelegate1::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
if (index.isValid()) {
int row = index.row();
QSqlQueryModel* Model = (QSqlQueryModel*)index.model();
QVariant data = index.data(Qt::DisplayRole);
WItemData d = data.value<WItemData>();
QRect rect = option.rect;
//qDebug() << rect;
int wx = 5;
int wy = 5;
rect.setX(rect.x() + wx);
rect.setY(rect.y() + wy);
rect.setWidth(rect.width() - wx * 2);
rect.setHeight(rect.height() - wy * 2);
//qDebug() << rect;
QString text = QString("rowid:%1,col1:%2,col2:%3").arg(d.rowid).arg(d.col1).arg(d.col2);
painter->setBrush(QColor(0, 0, 0));
painter->drawRoundedRect(rect, 5, 5);
painter->setPen(QColor(255, 255, 255));
painter->drawText(rect, text, QTextOption(Qt::AlignCenter));
}
}
#pragma once
#include <QAbstractListModel>
#include <QMetaType>
typedef struct {
int rowid;
int col1;
int col2;
} WItemData;
Q_DECLARE_METATYPE(WItemData)
class ListModel : public QAbstractListModel
{
Q_OBJECT
public:
ListModel(QObject *parent = nullptr);
~ListModel();
//当子类化QAbstractListModel时,必须提供rowCount()和data()函数的实现
virtual QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const;
virtual int rowCount(const QModelIndex& parent = QModelIndex()) const;
private:
QList<WItemData> m_List;
};
#include "ListModel.h"
ListModel::ListModel(QObject *parent)
: QAbstractListModel(parent)
{
for (int i = 0; i < 100000; i++)
{
WItemData data;
data.rowid = i;
data.col1 = i;
data.col2 = i;
m_List << data;
}
}
ListModel::~ListModel()
{}
QVariant ListModel::data(const QModelIndex& index, int role /*= Qt::DisplayRole*/) const
{
if (!index.isValid())
{
return QVariant();
}
if (Qt::DisplayRole == role)
{
WItemData data(m_List.at(index.row()));
QVariant variant;
variant.setValue<WItemData>(data);
return variant;
}
return QVariant();
}
int ListModel::rowCount(const QModelIndex& parent /*= QModelIndex()*/) const
{
return m_List.size();
}
ListModel* listModel = new ListModel;
listView = new QListView;
listView->setModel(listModel);
WItemDelegate1* wItemDelegate = new WItemDelegate1(listView);
listView->setItemDelegate(wItemDelegate);
listView->show();