QListview-QAbstractListModel-QStyledItemDelegate使用例子

本文介绍了一个自定义的QStyledItemDelegate,用于Qt模型视图框架中,展示了如何为列表项添加额外的样式和文本渲染,包括红色背景、文本定位和蓝色圆角。mylistModel继承自QAbstractListModel,用于数据管理,而mydeltgate则作为其样式代理,确保了不同角色的数据正确呈现。
摘要由CSDN通过智能技术生成
#include <QStyledItemDelegate>
#include <QPainter>
#include <QAbstractListModel>
#include <QStringList>
#include <QModelIndex>
#include <QLabel>

class mylistModel;

class mydeltgate : public QStyledItemDelegate
{
	Q_OBJECT

public:
	mydeltgate(QObject *parent = 0);
	~mydeltgate();
protected:
	void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index)const;
	QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const ;
	/*注意事项:记得在末尾加const ,保持和基类虚函数的写法一致,不一致不会触发该函数*/
	/*paint 需配合 sizehint一起使用*/
};

class mylistModel :public QAbstractListModel
{
	Q_OBJECT
public:
	explicit mylistModel(QObject *parent = 0);
	~mylistModel();

	int rowCount(const QModelIndex &parent) const;

	int columnCount(const QModelIndex &parent) const;

	QVariant data(const QModelIndex &index, int role) const;

	void add(QString str);

private:
	QStringList list;
	int m_testNumber;

signals:

	public slots :

};

cpp

mydeltgate::mydeltgate(QObject *parent)
	: QStyledItemDelegate(parent)
{
}

mydeltgate::~mydeltgate()
{
}
void mydeltgate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
	painter->save();
	QRect entityRect = option.rect;

	QBrush t_brush1QColor(Qt::red);
	painter->fillRect(entityRect, t_brush1QColor);

	painter->save();
	QRect rect1 = entityRect;
	rect1.setTop(entityRect.top() + 20);
	rect1.setBottom(entityRect.bottom() - 30);
	rect1.setLeft(entityRect.left() + 20);
	rect1.setRight(entityRect.right() - 20);
	painter->drawRect(rect1);

	painter->drawText(rect1.bottomLeft(), index.data(Qt::DisplayRole).toString());
	painter->save();
	QPainterPath path;
	path.addRect(entityRect.x() + 30, entityRect.y()+30,30,30);
	painter->setBrush(Qt::blue);
	painter->drawPath(path);
	painter->restore();//下次循环返回前一个save的painter
	/*注意事项:这里的定位需用传进来的option.rect来做定位,不能用绝对定位*/
	/*使用绝对定位时,每个index的数据都会重叠在该绝对定位*/
}
QSize mydeltgate::sizeHint(const QStyleOptionViewItem &option,const QModelIndex &index) const
{
	 return QSize(187 , 117 );
	 /*设置每行item的大小值*/
}

/*listmodel*/

mylistModel::mylistModel(QObject *parent) : QAbstractListModel(parent)
{
}

mylistModel::~mylistModel()
{
}

int mylistModel::rowCount(const QModelIndex &parent) const
{
/*行数*/
	return list.count();
}

int mylistModel::columnCount(const QModelIndex &parent) const
{
	/*列数*/
	return 1;
}

QVariant mylistModel::data(const QModelIndex &index, int role) const
{
/*这个是主要函数,委托根据index的对应角色取这里的对应角色值*/
/*不设置委托时,DisplayRole返回值直接显示在界面上*/
	if (role == Qt::BackgroundRole)
	{
		return  QBrush(Qt::green);
	}
	else if (role == Qt::DisplayRole)
	{
		return list.at(index.row());
	}
	else
	{
		return QVariant();
	}

}

void mylistModel::add(QString str)
{

	list.append(str);
}

main

mylistModel* ItemModel = new mylistModel(this);
mydeltgate*	delt = new mydeltgate(this);
	QStringList strList;
	strList.append("A");
	strList.append("B");
	strList.append("C");
	strList.append("D");
	strList.append("E");
	strList.append("F");
	strList.append("G");

	int nCount = strList.size();
	for (int i = 0; i < nCount; i++)
	{
		QString string = static_cast<QString>(strList.at(i));
		ItemModel->add(string);
	}


	ui.listView->setItemDelegate(delt);
	ui.listView->setModel(ItemModel);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值