首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >QPropertyAnimation在QRect中的应用

QPropertyAnimation在QRect中的应用
EN

Stack Overflow用户
提问于 2017-04-15 16:45:16
回答 1查看 919关注 0票数 0

我创建了一个QRect对象

代码语言:javascript
复制
QRect ellipse(10.0 , 10.0 , 10.0 , 10.0);
QPainter painter(this);
painter.setBrush(Qt::red);
painter.drawEllipse(ellipse);

现在我想使用QPropertyAnimation动画它,但是由于它只能应用于QObject对象(就我所知),我需要以某种方式将QRect转换为QObject。有办法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-15 20:40:55

不需要创建类,您可以使用自己的小部件,您必须添加一个新的属性。

示例:

widget.h

代码语言:javascript
复制
#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

widget.cpp

代码语言:javascript
复制
#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);
}

代码

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43428627

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档