欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

QT学习---listWidget自绘

程序员文章站 2022-05-28 21:48:44
...

一、自绘的主要步骤是:

1、派生QStyledItemDelegate的子类;

2、重写虚函数:sizeHint()、paint();

3、设置listWidget的委托对象。

ui.listWidget->setItemDelegate(new MyItemPainter(ui.listWidget));

二、实现:

1、项listWidget部件中添加些测试数据:

ListWidget::ListWidget(QWidget *parent, Qt::WFlags flags)
	: QMainWindow(parent, flags)
{
	ui.setupUi(this);

	
	QListWidgetItem* item1 = new QListWidgetItem;
	QListWidgetItem* item2 = new QListWidgetItem;

	item1->setData(Qt::DecorationRole,QIcon(":/ListWidget/Resources/Save.png"));
	item1->setData(Qt::DisplayRole,"Save");
	item1->setData(Qt::UserRole + 1,"This is Icon of Sage");
	item1->setData(Qt::UserRole + 2,":/ListWidget/Resources/Save.png");

	item2->setData(Qt::DecorationRole,QIcon(":/ListWidget/Resources/grab.png"));
	item2->setData(Qt::DisplayRole,"Grab");
	item2->setData(Qt::UserRole + 1,"This is Icon of Grab");
	item2->setData(Qt::UserRole + 2,":/ListWidget/Resources/grab.png");

	ui.listWidget->addItem(item1);
	ui.listWidget->addItem(item2);

	ui.listWidget->setItemDelegate(new MyItemPainter(ui.listWidget));
	
}

2、在派生的子类中进行重绘操作:

#ifndef MYITEMPAINTER_H
#define MYITEMPAINTER_H

#include <QStyledItemDelegate>

class MyItemPainter : public QStyledItemDelegate
{
	Q_OBJECT

public:
	MyItemPainter(QWidget *parent);
	~MyItemPainter();

private:
	QSize sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const;
	void paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const;
};

#endif // MYITEMPAINTER_H
#include "MyItemPainter.h"
#include <QPainter>

MyItemPainter::MyItemPainter(QWidget *parent)
	: QStyledItemDelegate(parent)
{

}

MyItemPainter::~MyItemPainter()
{

}

QSize MyItemPainter::sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const
{
	//获取每一行的原始大小
	QSize size = QStyledItemDelegate::sizeHint(option,index);
	size.setHeight(60);
	return size;

	//重写之前的默认返回值
	//return QStyledItemDelegate::sizeHint(option,index);
}

void MyItemPainter::paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
{
	QRect Rect = option.rect;
	Rect.adjust(2,2,-2,-2);

	//取得对应项的数据
	QString strDisplay = index.data(Qt::DisplayRole).toString();//名称
	QString strTip = index.data(Qt::UserRole + 1).toString();//提示
	QString strPath = index.data(Qt::UserRole + 2).toString();//图标路径

	//显示操作:当选中某一项时
	if (option.state & QStyle::State_Selected)
	{
		painter->setBrush(QColor(50,183,183));
		painter->drawRoundedRect(Rect,2,2);
		painter->setBrush(Qt::NoBrush);
	}

	//画名称
	{
		QRect dst = Rect;
		dst.setLeft(dst.left()+ 10);
		dst.setRight(dst.left()+40);
		dst.setTop(dst.top()+8);
		dst.setBottom(dst.bottom()-40);
		painter->drawText(dst, Qt::AlignLeft | Qt::AlignVCenter, strDisplay);
	}

	//画图标
	{
		QPixmap pmap(strPath);
		QRect dst = Rect;
		dst.setTop(dst.top()+20);
		dst.setRight(dst.left()+40);
		QRect area(0,0,24,24);
		area.moveCenter(dst.center());
		painter->drawPixmap(area,pmap);
	}

	//画提示
	{
		QRect dst = Rect;
		dst.setLeft(dst.left()+45);
		dst.setTop(dst.top()+20);
		painter->drawText(dst, Qt::AlignLeft | Qt::AlignVCenter, strTip);
	}

	//重写之前的默认返回值
	//return QStyledItemDelegate::paint(painter,option,index);
}

QT学习---listWidget自绘