将QPropertyAnimation应用于QRect

时间:2017-04-15 16:45:17

标签: c++ qt qt5

我已经创建了一个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。有办法吗?

1 个答案:

答案 0 :(得分:1)

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

示例:

<强> widget.h

#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.cpp

'use strict';
Vue.component("book-chapter", Vue.extend({
  name: "book-chapter",
  props: ["data", "current-depth"],
  data: function() {
    return {
      checked: this.data.checked,
      indeterminate: this.data.indeterminate || false
    };
  },
  methods: {
    isChecked: function() {
      return this.checked && !this.indeterminate;
    },
    isIndeterminate: function(){
      return this.indeterminate;
    },
    toggleCheckbox: function(eventData) {
      if (this.currentDepth > 0){

        if (!this.data.children) {
          this.checked != this.children
        } else {
          this.indeterminate = !this.indeterminate;
        }
      }

      if (eventData) {
        // fired by nested chapter
        this.$emit('checked', eventData);

      } else {
        // fired by top level chapter
        this.checked = !this.checked;
        this.$emit('checked', {
          data: this.data
        });
      }
    },
    isRootObject: function() {
      return this.currentDepth === 0;
    },
    isChild: function() {
      return this.currentDepth === 2;
    },
    isGrandChild: function() {
      return this.currentDepth > 2;
    }
  },
  template: `
  <div class='book__chapters'>
   <div
      class='book__chapter'
      v-bind:class="{ 'book__chapter--sub': isChild(), 'book__chapter--subsub': isGrandChild() }"
      v-show='!isRootObject()'>
      <div class='book__chapter__color'></div>
      <div
         class='book__chapter__content'
         v-bind:class="{ 'book__chapter__content--sub': isChild(), 'book__chapter__content--subsub': isGrandChild() }">
         <div class='book__chapter__title'>
            <span class='book__chapter__title__text'>{{data.title}}</span>
         </div>
         <div class='book__chapter__checkbox triple-checkbox'>
            <div class='indeterminatecheckbox'>
               <div
                  class='icon'
                  @click.stop="toggleCheckbox()"
                  v-bind:class="{'icon--checkbox-checked': isChecked(), 'icon--checkbox-unchecked': !isChecked(), 'icon--checkbox-indeterminate': isIndeterminate()}">
               </div>
            </div>
         </div>
      </div>
   </div>
   <book-chapter
      ref='chapter'
      :current-depth='currentDepth + 1'
      v-for='child in data.children'
      key='child.id'
      @checked='toggleCheckbox(arguments[0])'
      :data='child'>
   </book-chapter>
</div>
`
}));

Vue.component("book", Vue.extend({
  name: "book",
  props: ["data"],
  template: `
    <div class='book'>
      <book-chapter 
        :data='this.data'
        :currentDepth='0'>
      </book-chapter>
    </div>
`
}));

var parent = new Vue({
  el: "#container",
  data: function() {
    return {
      book: {}
    };
  },
  mounted: function() {
    this.book = {
      "title": "Book",
      "children": [{
        "title": "1 First title",
        "children": [{
          "title": "1.1 Subtitle"
        }, {
          "title": "1.2 Subtitle"
        }]
      }, {
        "title": "2 Second title",
        "children": [{
          "title": "2.1 Subtitle",
          "children": [{
            "title": "2.1.1 Sub-Sub title"
          }, {
            "title": "2.1.2 Another sub-sub title"
          }]
        }]
      }]
    }
  }
});

code