QTQQ项目总结(三)---联系人根项三角形旋转(属性动画)

一.当点击联系人根项时,根项三角形旋转

请添加图片描述

二. 项目应用


#include <QLabel>
#include <QPropertyAnimation>

class RootContatItem : public QLabel
{
	Q_OBJECT

	//箭头角度
	Q_PROPERTY(int rotation READ rotation WRITE setRotation)
public:
	RootContatItem(bool hasArrow = true,QWidget *parent = nullptr);
	~RootContatItem();

public:
	void setText(const QString& title);
	void setExpanded(bool expand);

private:
	int rotation();//旋转
	void setRotation(int rotation);

protected:
	void paintEvent(QPaintEvent* event);//绘画事件

private:
	QPropertyAnimation *m_animation;	//属性动画
	QString m_titleText;		//显示的文本
	int m_rotation;			//箭头的角度
	bool m_hasArrow;		//是否有箭头
};

#include "RootContatItem.h"
#include <QPainter>

RootContatItem::RootContatItem(bool hasArrow,QWidget *parent)
	: QLabel(parent)
	,m_rotation(0)			//初始化角度0
	,m_hasArrow(hasArrow)//初始化是否有箭头
{
	setFixedHeight(32);//将小部件的最小高度和最大高度都设置为32
	setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

	//初始![请添加图片描述](https://img-blog.csdnimg.cn/de364f002ec8430b80fa7a9f99f61d9d.png)
(旋转属性)
	m_animation = new QPropertyAnimation(this, "rotation");	//旋转属性
	m_animation->setDuration(50);												//设置单次动画时长50ms
	m_animation->setEasingCurve(QEasingCurve::InQuad);			//动画缓和曲线类型 t^2从0加速型 t^2从0加速
}

RootContatItem::~RootContatItem()
{
}

void RootContatItem::setText(const QString & title)
{
	m_titleText = title;
	update();
}

void RootContatItem::setExpanded(bool expand)
{
	if (expand)
	{
		m_animation->setEndValue(90);//设置属性动画的结束值
	}
	else
	{
		m_animation->setEndValue(0);
	}

	m_animation->start();
}

int RootContatItem::rotation()
{
	return m_rotation;
}

void RootContatItem::setRotation(int rotation)
{
	m_rotation = rotation;
	update();
}

void RootContatItem::paintEvent(QPaintEvent * event)
{
	QPainter painter(this);
	painter.setRenderHint(QPainter::TextAntialiasing, true);

	QFont font;
	font.setPointSize(10);
	painter.setFont(font);
	painter.drawText(24, 0, width() - 24, height(), Qt::AlignLeft | Qt::AlignVCenter, m_titleText);
	painter.setRenderHint(QPainter::SmoothPixmapTransform, true);
	painter.save();	//保存画家设置

	if (m_hasArrow)
	{
		QPixmap pixmap;
		pixmap.load(":/Resources/MainWindow/arrow.png");

		QPixmap tmpPixmap(pixmap.size());
		tmpPixmap.fill(Qt::transparent);

		QPainter p(&tmpPixmap);
		p.setRenderHint(QPainter::SmoothPixmapTransform, true);

		//坐标系偏移(x方向偏移,y反向偏移)
		p.translate(pixmap.width() / 2, pixmap.height() / 2);
		p.rotate(m_rotation);	//旋转坐标系(顺时针)
		p.drawPixmap(0 - pixmap.width() / 2, 0 - pixmap.height() / 2, pixmap);

		painter.drawPixmap(6, (height() - pixmap.height()) / 2, tmpPixmap);
		painter.restore();	//恢复画家设置
	}

	QLabel::paintEvent(event);
}

(1) enum QSizePolicy::Policy

此枚举描述构造QSizePolicy时使用的各种按维度大小调整类型。
在这里插入图片描述

(2) enum QEasingCurve::Type

缓和曲线的类型。
在这里插入图片描述

(3) enum QPainter::RenderHint

flags QPainter::RenderHints

Renderhint用于为QPainter指定任何给定引擎都可能遵守或不遵守的标志。
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

江凡心

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值