Hi.
I have a small testproject, where I have painted a roundedrect box, that I want to animate. My problem is that I can't get the animation to run smoothly.
The painted box is animated with a duration of 1000ms, and "stutters" frequently while moving across the dialog.
This is the code for the roundedrect box:
#include "groupbox.h"
#include <QPainter>
#include <QDebug>
GroupBox
::GroupBox(QWidget *parent
){
}
GroupBox::~GroupBox()
{
}
{
painter.begin(this);
painter.
setRenderHint(QPainter::Antialiasing);
painter.
setPen(QPen(QBrush(qRgb
(128,
128,
128)),
5));
painter.
setBrush(QBrush(qRgb
(46,
46,
46)));
painter.
drawRoundedRect(QRect(35,
35,
250,
150),
25,
25);
painter.end();
}
#include "groupbox.h"
#include <QPainter>
#include <QDebug>
GroupBox::GroupBox(QWidget *parent)
: QWidget(parent)
{
}
GroupBox::~GroupBox()
{
}
void GroupBox::paintEvent(QPaintEvent *)
{
QPainter painter;
painter.begin(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.setPen(QPen(QBrush(qRgb(128,128,128)), 5));
painter.setBrush(QBrush(qRgb(46,46,46)));
painter.drawRoundedRect(QRect(35,35,250,150),25,25);
painter.end();
}
To copy to clipboard, switch view to plain text mode
And here is the code for the animation part:
#include "dialog.h"
#include <QDebug>
{
resize(1400,600);
m_group = new GroupBox(this);
m_group->resize(310,200);
m_group->show();
m_animation = new QPropertyAnimation(m_group, "pos", this);
m_animation->setDuration(1000);
m_animation->setEasingCurve(QEasingCurve::Linear);
// Continuous animation from startup
m_group->move(10, 0);
animateMove();
connect(m_animation, SIGNAL(finished()), this, SLOT(animateMove()));
}
Dialog::~Dialog()
{
}
void Dialog::animateMove()
{
if (m_group->pos().x() != 10)
{
m_animation->setStartValue(m_group->pos());
m_animation
->setEndValue
(QPointF(10.0,
0.0));
m_animation->start();
}
else
{
m_animation->setStartValue(m_group->pos());
m_animation
->setEndValue
(QPointF(1000.0,
10.0));
m_animation->start();
}
}
#include "dialog.h"
#include <QDebug>
Dialog::Dialog(QWidget *parent)
: QDialog(parent)
{
resize(1400,600);
m_group = new GroupBox(this);
m_group->resize(310,200);
m_group->show();
m_animation = new QPropertyAnimation(m_group, "pos", this);
m_animation->setDuration(1000);
m_animation->setEasingCurve(QEasingCurve::Linear);
// Continuous animation from startup
m_group->move(10, 0);
animateMove();
connect(m_animation, SIGNAL(finished()), this, SLOT(animateMove()));
}
Dialog::~Dialog()
{
}
void Dialog::animateMove()
{
if (m_group->pos().x() != 10)
{
m_animation->setStartValue(m_group->pos());
m_animation->setEndValue(QPointF(10.0,0.0));
m_animation->start();
}
else
{
m_animation->setStartValue(m_group->pos());
m_animation->setEndValue(QPointF(1000.0,10.0));
m_animation->start();
}
}
To copy to clipboard, switch view to plain text mode
This has been tested on Linux and Windows with debug and release having the same result.
Thanks in advance for any help/hints!
Bookmarks