带有框架的 QGraphicsTextItem 的 QPropertyAnimation 使文本不稳定

QPropertyAnimation of a QGraphicsTextItem with a frame make the text shaky

我正在制作 QGraphicsTextItem 的动画,我在周围添加了一个框架。在动画过程中,文字似乎在框架内轻微晃动,这很烦人。

示例代码:

class MovingFrameText : public QGraphicsTextItem
{
    Q_OBJECT;

   public:
      MovingFrameText( ) : QGraphicsTextItem(0)
      {
         setPlainText ( "human ");
         QFont f =  font();
         f.setPixelSize(40);
         setFont(f);
         setFlags(QGraphicsItem::ItemIsMovable);
      }

      QRectF boundingRect() const
      {
         return QGraphicsTextItem::boundingRect().adjusted(-2,-2,+2,+2);
      }

      void paint(QPainter *painter,const QStyleOptionGraphicsItem *option,QWidget *widget)
      {
         QGraphicsTextItem::paint(painter,option,widget);
         painter->setPen(Qt::black);
         painter->drawRect(boundingRect());
      }

};

int main(int argc, char *argv[])
{
   QApplication app(argc,argv);


   MovingFrameText t;
   t.setPos(640,680);

   QGraphicsScene scene;
   scene.addItem(&t);

   QGraphicsView view(&scene);
   view.resize(640, 680);
   view.show();

    auto moveAnimation = new QPropertyAnimation( &t,  "pos" );
    moveAnimation->setDuration( 10000 );
    moveAnimation->setStartValue( QPointF(640, 680) );
    moveAnimation->setEndValue(  QPointF(0, 0) );
    moveAnimation->setEasingCurve( QEasingCurve::Linear );
    moveAnimation->start(QAbstractAnimation::DeleteWhenStopped);

 return app.exec();
}

有什么办法可以让动画流畅吗?

解决方案

您可以通过以下方式显着改进动画:

  1. 每次迭代使用 QVariantAnimation instead of QPropertyAnimation and calling QGraphicsItem::update
  2. 使用额外的 QPainterQPixmap 作为所有绘画操作的 canvas 缓冲绘画,然后使用传递给 [的画家绘画 canvas =16=]方法

注意:QGraphicsTextItem还是会有一点晃动,但至少它会像一个物体而不是几个独立的物体。

例子

这是我为您准备的示例,说明如何更改您的代码以实施建议的解决方案:

class MovingFrameText : public QGraphicsTextItem
{
public:
    MovingFrameText(const QString &text, QGraphicsItem *parent = nullptr)
        : QGraphicsTextItem(parent)
    {
        QFont f(font());

        f.setPixelSize(40);

        setFont(f);
        setPlainText(text);
    }

    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    {
        painter->setClipping(true);
        painter->setClipRect(option->rect);
        painter->setRenderHint(QPainter::SmoothPixmapTransform);

        QPixmap canvas(option->rect.size());
        QPainter canvasPainter;

        canvas.fill(Qt::transparent);

        canvasPainter.begin(&canvas);

        canvasPainter.setFont(font());
        canvasPainter.drawRect(option->rect.adjusted(0, 0, -1, -1));
        canvasPainter.drawText(option->rect, toPlainText());

        painter->drawPixmap(0, 0, canvas);
    }
};

int main(int argc, char *argv[])
{
    QApplication app(argc,argv);

    QGraphicsView view;
    auto *t = new MovingFrameText("human");

    view.setScene(new QGraphicsScene(&view));
    view.setAlignment(Qt::AlignLeft | Qt::AlignTop);
    view.setSceneRect(0, 0, 640, 680);
    view.scene()->addItem(t);
    view.show();

    auto *moveAnimation = new QVariantAnimation();

    moveAnimation->setDuration(10000);
    moveAnimation->setStartValue(QPointF(640, 680));
    moveAnimation->setEndValue(QPointF(0, 0));
    moveAnimation->start(QAbstractAnimation::DeleteWhenStopped);

    QObject::connect(moveAnimation, &QVariantAnimation::valueChanged, [t](const QVariant &value){
        t->setPos(value.toPointF());
        t->update();
    });

    return app.exec();
}

@scopchanov 的回答非常好。我在这里添加了一个我也测试过的解决方案,并且似乎有效。我将需要更多时间来检查我认为哪个更好,但如果您遇到类似问题,我会把它留在这里供其他人尝试。

class MovingFrameText : public QGraphicsTextItem
{

   Q_PROPERTY( QPointF pos READ pos WRITE setPosition )
   public:

    void setPosition(QPointF pos)
    {
       setPos(floor(pos.x()+0.5),floor(pos.y()+.5) );
    }

};