将 QPropertyAnimation 应用于 QRect
Applying QPropertyAnimation to QRect
我创建了一个 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。有没有办法做到这一点?
无需创建类,您可以使用自己的小部件,必须添加新属性。
例:
小部件.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
小部件.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);
}
法典
相关文章:
- 使用 fopen 打开 .pak 文件并使该文件应用于 const 无符号字符* (C++)
- 使用 std::应用于 std::bind
- 如何将Affine3d应用于Vector3d
- 运算符 += 应用于 std::valarray<int*>
- 函数可以应用于 std::optional,并返回一个可选值吗?
- 为什么带有类型参数的运算符 () 可以应用于 result_of 上下文中的类型?
- 当正斜杠运算符应用于C++中的字符串类型时,它会做什么
- 将MFPKEY_ASFMEDIASINK_AUTOADJUST_BITRATE应用于IMFSinkWriter
- (C++)(Visual Studio) 将高斯模糊滤镜应用于 RGB 中的灰度图像
- 在运行时将更改的环境变量应用于 QApplication
- Strcmp 类行为应用于C++中的对象列表
- 如何将 ItemDelegate 设置为仅应用于 QTreeView 中的父列
- 什么是应用于C 和CMAKE的定义
- 将对象方法从向量应用于C 的第二个向量中的对象
- STL:将功能应用于Adchacent_difference的结果,而无需额外的容器
- WXWIDGETS应用于WXStaticBitMap时,屏幕截图将大部分图像留为空白
- 如何将垂直纹理应用于QT Surface3D
- 如何将[[nodiscard]]属性应用于lambda
- 奇怪的语法构造 - 应用于 typedef 的运算符 () 使用 VS2015 成功编译
- 将 QPropertyAnimation 应用于 QRect