我创建了一个QRect对象
QRect ellipse(10.0 , 10.0 , 10.0 , 10.0);
QPainter painter(this);
painter.setBrush(Qt::red);
painter.drawEllipse(ellipse);现在我想使用QPropertyAnimation动画它,但是由于它只能应用于QObject对象(就我所知),我需要以某种方式将QRect转换为QObject。有办法吗?
发布于 2017-04-15 20:40:55
不需要创建类,您可以使用自己的小部件,您必须添加一个新的属性。
示例:
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_Hwidget.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);
}https://stackoverflow.com/questions/43428627
复制相似问题