继承QAbstractListModel,结合QListView

这里想要写一个QAbstractListModel的子类,学习一下如何实例化QAbstractListModel。

QAbstractListModel子类化-CSDN博客

QVariant与自定义类型互转之奇巧淫技_qt 类型转 qvariant-CSDN博客

cpp 复制代码
#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;
};
cpp 复制代码
#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));
    }
}
cpp 复制代码
#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;
};
cpp 复制代码
#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();
}
cpp 复制代码
    ListModel* listModel = new ListModel;
    listView = new QListView;
    listView->setModel(listModel);
    WItemDelegate1* wItemDelegate = new WItemDelegate1(listView);
    listView->setItemDelegate(wItemDelegate);
    listView->show();
相关推荐
Felix_One20 小时前
Qt 串口通信避坑指南:QSerialPort 的 5 个常见问题
qt
blasit4 天前
笔记:Qt C++建立子线程做一个socket TCP常连接通信
c++·qt·tcp/ip
郑州光合科技余经理9 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo1239 天前
matlab画图工具
开发语言·matlab
dustcell.9 天前
haproxy七层代理
java·开发语言·前端
norlan_jame9 天前
C-PHY与D-PHY差异
c语言·开发语言
多恩Stone9 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
QQ4022054969 天前
Python+django+vue3预制菜半成品配菜平台
开发语言·python·django
遥遥江上月9 天前
Node.js + Stagehand + Python 部署
开发语言·python·node.js
m0_531237179 天前
C语言-数组练习进阶
c语言·开发语言·算法