C++ 将QPropertyAnimation应用于QRect
我已经创建了一个QRect对象C++ 将QPropertyAnimation应用于QRect,c++,qt,qt5,C++,Qt,Qt5,我已经创建了一个QRect对象 QRect ellipse(10.0 , 10.0 , 10.0 , 10.0); QPainter painter(this); painter.setBrush(Qt::red); painter.drawEllipse(ellipse); 现在我想使用QPropertyImation为其设置动画,但由于它只能应用于QObject对象(据我所知),因此我需要以某种方式将QRect转换为QObject。有办法吗?无需创建类,您可以使用自己的小部件,您必须添加新
QRect ellipse(10.0 , 10.0 , 10.0 , 10.0);
QPainter painter(this);
painter.setBrush(Qt::red);
painter.drawEllipse(ellipse);
现在我想使用QPropertyImation为其设置动画,但由于它只能应用于QObject对象(据我所知),因此我需要以某种方式将QRect转换为QObject。有办法吗?无需创建类,您可以使用自己的小部件,您必须添加新属性 例如: widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QPaintEvent>
#include <QWidget>
class Widget : public QWidget
{
Q_OBJECT
Q_PROPERTY(QRect nrect READ nRect WRITE setNRect)
public:
explicit Widget(QWidget *parent = 0);
~Widget();
QRect nRect() const;
void setNRect(const QRect &rect);
protected:
void paintEvent(QPaintEvent *event);
private:
QRect mRect;
};
#endif // WIDGET_H
#ifndef小部件
#定义小部件
#包括
#包括
类Widget:publicqwidget
{
Q_对象
Q_属性(QRect nrect READ nrect WRITE setNRect)
公众:
显式小部件(QWidget*parent=0);
~Widget();
QRect nRect()常量;
void setNRect(const QRect&rect);
受保护的:
无效油漆事件(QPaintEvent*事件);
私人:
QRect-mRect;
};
#endif//WIDGET\u H
widget.cpp
#include "widget.h"
#include <QPainter>
#include <QPropertyAnimation>
Widget::Widget(QWidget *parent) :
QWidget(parent)
{
QPropertyAnimation *animation = new QPropertyAnimation(this, "nrect");
//animation->setEasingCurve(QEasingCurve::InBack);
animation->setDuration(1000);
animation->setStartValue(QRect(0, 0, 10, 10));
animation->setEndValue(QRect(0, 0, 200, 200));
animation->start();
connect(animation, &QPropertyAnimation::valueChanged, [=](){
update();
});
}
Widget::~Widget()
{
}
QRect Widget::nRect() const
{
return mRect;
}
void Widget::setNRect(const QRect &rect)
{
mRect = rect;
}
void Widget::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event)
QRect ellipse(mRect);
QPainter painter(this);
painter.setBrush(Qt::red);
painter.drawEllipse(ellipse);
}
#包括“widget.h”
#包括
#包括
Widget::Widget(QWidget*父项):
QWidget(父级)
{
QPropertyAnimation*动画=新的QPropertyAnimation(此“nrect”);
//动画->设定曲线(QEasingCurve::InBack);
动画->设置持续时间(1000);
动画->设置起始值(QRect(0,0,10,10));
动画->setEndValue(QRect(0,0,200,200));
动画->开始();
连接(动画,&QPropertyAnimation::valueChanged,[=](){
更新();
});
}
小部件::~Widget()
{
}
QRect小部件::nRect()常量
{
返回mRect;
}
void小部件::setNRect(常量QRect和rect)
{
mRect=rect;
}
void小部件::paintEvent(QPaintEvent*事件)
{
Q_未使用(事件)
QRect椭圆(mRect);
油漆工(本);
画家。挫折(Qt::红色);
画家。抽屉(椭圆);
}
您可以更好地向我解释一下,为了给您提供另一个选项,您想做些什么。我正在尝试创建基于浮点数据数组的均衡器。我在考虑做圆圈,而不是用规则的条纹和线条把它们连接起来。非常感谢,这正是我想要的!