一.当点击联系人根项时,根项三角形旋转
二. 项目应用
#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指定任何给定引擎都可能遵守或不遵守的标志。