开发者

Qt animation problem - flickering geometry animation

开发者 https://www.devze.com 2023-03-02 02:11 出处:网络
I have a problem creating a simple animation with Qt animation. I have a small image located at the bottom right corner of the screen, i am trying to create animation that will enlarge the picture by

I have a problem creating a simple animation with Qt animation. I have a small image located at the bottom right corner of the screen, i am trying to create animation that will enlarge the picture by streching it from the the top left corner of the image and streching it to the center of the screen. I managed to do that, but, it's very notiable that qanimation makes it flicker (the right border of the picture, and it doesn't turns out good) I also did that with no animation, but wi开发者_如何学JAVAth a timer, and changing window geometry , but i had the same problem, seems that it's not refreshing fast enough, creating flickering in the right border of the picture.

here are the 2 examples:

1 - using property animation (geometery)

animation = new QPropertyAnimation(this, "geometry");
animation->setDuration(555);
animation->setEasingCurve(QEasingCurve::Linear);
animation->setStartValue(QRect(availableScreenSize.width()  -minWidth
    -WINDOW_PADDING,availableScreenSize.height() - minHeight
    -WINDOW_PADDING,minWidth,minHeight));
animation->setEndValue(QRect(availableScreenSize.width() - maxWidth
    -WINDOW_PADDING,availableScreenSize.height() - maxHeight
    -WINDOW_PADDING,maxWidth,maxHeight));

2 - using a timer

#include "widget.h"
#include "ui_widget.h"
#include <QDesktopWidget>
#include <QDebug>

Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->setStyleSheet("background:transparent;");
    this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint
        | Qt::Tool);

    availableScreenSize = qApp->desktop()->availableGeometry();

    //Growing from right to left
    this->setGeometry(availableScreenSize.width() - 165,
        availableScreenSize.height()-95,160,90);
    //Growing from left to right
    //this->setGeometry(200,200,160,90);

    timeLine = new QTimeLine();
    timeLine->setDuration(2222);
    timeLine->setFrameRange(1, 800);
    connect(timeLine, SIGNAL(frameChanged(int)), this, SLOT(update()));

    counter = 0;

    timeLine->start();
}

Widget::~Widget()
{
    delete ui;
}

void Widget: aintEvent(QPaintEvent * /* event */)
{
    counter++;
    qDebug() << counter;

    qApp->processEvents();
    //Growing from right to left
    this->setGeometry(availableScreenSize.width()  -165 
        - this->width()-1,availableScreenSize.height() - 95
        - this->height()-1,this->width()+1,this->height()+1);

    //Growing from left to right
    //this->setGeometry(200,200,this->width()+1,this->height()+1);

    if(timeLine->currentFrame() == 800)
    {
        qApp->exit(1);
    }
}

Now the weired thing here is that if the animation is from left 2 right - it looks smooth... once the direction is changed from right 2 left the entire right border is "jumpy" .

I'll appriciate any help that you can give me. Thanks!


I'm experiencing the same problem currently with animating scaling of my QGraphicsPixmapItem. Unlike your case, I'm using setScale(scaleValue) instead of setGeometry because that method is not available for QGraphicsPixmapItem. I was able to remove the flicker using the following code (Note: I'm using PySide binding for QT but you should be able to find the corresponding C++ methods easily):

In my initializer for my QGraphicsPixmapItem class, I called:

self.setTransformationMode(Qt.SmoothTransformation)
self.setCacheMode(PySide.QtGui.QGraphicsItem.DeviceCoordinateCache)

In my QGraphicsView:

self.setCacheMode(QGraphicsView.CacheBackground)
self.setRenderHints(PySide.QtGui.QPainter.Antialiasing | PySide.QtGui.QPainter.SmoothPixmapTransform)

Even though the flicker is gone, the animation is still a bit jerky.

If anyone has a better solution, would really appreciate it if you could post it. Thanks.


For your parent widget and on the widget on where animation is happening you can set this flag :

parentWidget.setAttribute(Qt::WA_NoSystemBackground); animatingWidget.setAttribute(Qt::WA_NoSystemBackground);

0

精彩评论

暂无评论...
验证码 换一张
取 消